2011-02-03 13 views
5

मैं अपने यूआईएममेज में गोलाकार कोनों को जोड़ने के लिए निम्नलिखित कोड का उपयोग कर रहा हूं, लेकिन समस्या यह है कि गोलाकार कोनों पारदर्शी या "स्पष्ट" के बजाय "सफेद" क्षेत्र दिखा रहे हैं। क्या मैं गलत कर रहा हूँ यहाँ:पारदर्शी गोलाकार कोनों के साथ UIImage

- (UIImage *)makeRoundCornerImageWithCornerWidth:(int)cornerWidth cornerHeight:(int)cornerHeight { 
    UIImage * newImage = nil; 

    if (self != nil) { 
     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 
     int w = self.size.width; 
     int h = self.size.height; 

     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
     CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst); 

     CGContextBeginPath(context); 
     CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height); 
     [self addRoundedRectToPath:context rect:rect width:cornerWidth height:cornerHeight]; 

     CGContextClosePath(context); 
     CGContextClip(context); 

     CGContextDrawImage(context, CGRectMake(0, 0, w, h), self.CGImage); 
     CGImageRef imageMasked = CGBitmapContextCreateImage(context); 
     CGContextRelease(context); 
     CGColorSpaceRelease(colorSpace); 

     newImage = [[UIImage imageWithCGImage:imageMasked] retain]; 
     CGImageRelease(imageMasked); 

     [pool release]; 
    } 

    return [newImage autorelease]; 
} 


- (void)addRoundedRectToPath:(CGContextRef)context rect:(CGRect)rect width:(float)ovalWidth height:(float)ovalHeight { 
    float fw, fh; 

    // If the width or height of the corner oval is zero, then it reduces to a right angle, 
    // so instead of a rounded rectangle we have an ordinary one. 
    if (ovalWidth == 0 || ovalHeight == 0) { 
     CGContextAddRect(context, rect); 
     return; 
    } 

    // Save the context's state so that the translate and scale can be undone with a call 
    // to CGContextRestoreGState. 
    CGContextSaveGState(context); 

    // Translate the origin of the contex to the lower left corner of the rectangle. 
    CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect)); 

    //Normalize the scale of the context so that the width and height of the arcs are 1.0 
    CGContextScaleCTM (context, ovalWidth, ovalHeight); 

    // Calculate the width and height of the rectangle in the new coordinate system. 
    fw = CGRectGetWidth (rect)/ovalWidth; 
    fh = CGRectGetHeight (rect)/ovalHeight; 

    // CGContextAddArcToPoint adds an arc of a circle to the context's path (creating the rounded 
    // corners). It also adds a line from the path's last point to the begining of the arc, making 
    // the sides of the rectangle. 
    CGContextMoveToPoint(context, fw, fh/2);    // Start at lower right corner 
    CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1); // Top right corner 
    CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1);  // Top left corner 
    CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1);  // Lower left corner 
    CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); // Back to lower right 

    // Close the path 
    CGContextClosePath(context); 

    // Restore the context's state. This removes the translation and scaling 
    // but leaves the path, since the path is not part of the graphics state. 
    CGContextRestoreGState(context); 
} 
+0

क्यों आप अपने दृश्य की परत करने के लिए 'cornerRadius' में शामिल न करें और masksToBounds सेट करने के लिए हाँ ... कि गोलाकार कोनों दिखाना चाहिए के लिए एक नौकरी की तरह लगता है .. – lukya

+0

यह पढ़ने के लिए एक महान पोस्ट है, कोड के साथ - http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/ – petert

+0

@lukya, हाँ मैं कर सकता हूँ , और मेरे पास है, लेकिन मैं सिर्फ कोड के इस टुकड़े के साथ इस मुद्दे की जांच करना चाहता था। – Mustafa

उत्तर

11

यहाँ UIKit कॉल का उपयोग कर एक सरल तैयार है:

- (UIImage*) roundCorneredImage: (UIImage*) orig radius:(CGFloat) r { 
    UIGraphicsBeginImageContextWithOptions(orig.size, NO, 0); 
    [[UIBezierPath bezierPathWithRoundedRect:(CGRect){CGPointZero, orig.size} 
           cornerRadius:r] addClip]; 
    [orig drawInRect:(CGRect){CGPointZero, orig.size}]; 
    UIImage* result = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return result; 
} 

सूचना नहीं पैरामीटर - इस छवि संदर्भ पारदर्शी बनाता है, इसलिए काटा-आउट क्षेत्र पारदर्शी है।

1

अधिकार के साथ स्पष्ट बिटमैप संदर्भ बनाने के बाद: अपने प्रश्न नीचे

CGContextClearRect (context, CGRectMake(0, 0, w, h)); 
1

lukya की टिप्पणी है कि आप शायद क्या करना चाहते हैं क्या।

सुनिश्चित करें कि आप QuartzCore आयात करें:

#import <QuartzCore/QuartzCore.h> 

तो फिर, अगर आप अपनी छवि का एक UIImageView है कि आप गोलाकार कोनों करना चाहते है, बस फोन (यह मानते हुए imageView एक संपत्ति है और cornerRadius वांछित कोने की त्रिज्या है):

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithCGImage:self.CGImage]]; 
:

self.imageView.layer.cornerRadius = cornerRadius; 
self.imageView.clipsToBounds = YES; 

के बाद से आप पहले से ही self.CGImage है, तो आप एक UIImageView बनाने के लिए ऐसा कर सकता है

बस इसे सबव्यूव के रूप में जोड़ने के बाद छवि दृश्य को रिलीज़ करना सुनिश्चित करें।

1
profileImageView.layer.cornerRadius = profileImageView.frame.size.height/2; 
profileImageView.clipsToBounds = YES; 
+1

केवल नंगे कोड पोस्ट करने की बजाय, यह मदद करेगा यदि आपने कुछ स्पष्टीकरण जोड़ा है कि आपका समाधान सबसे अच्छा क्यों है, या यह समस्या को हल कैसे करता है। –

संबंधित मुद्दे