2011-02-03 19 views
5

私はUIImageに丸いコーナーを追加するために次のコードを使用していますが、丸みを帯びたコーナーが透明または "透明"の代わりに "白い"領域を表示しています。私はここで間違って何をやっている:透明な丸いコーナーを持つ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

のための仕事のように聞こえる...それは丸みを帯びた角を示す必要があります。.. – 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; 
} 

お知らせNOパラメーターは - これは、画像コンテキストが透明になりませんので、クリッピングアウト領域が透明です。

1

右とその明確なビットマップのコンテキストを作成した後:あなたの質問以下

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

lukyaさんのコメントは、おそらく何をしたいです。

あなたはQuartzCoreインポートしていることを確認してください:あなたは、丸い角を持つようにしたいあなたのイメージのUIImageViewを持っている場合

#import <QuartzCore/QuartzCore.h> 

その後、呼び出すだけで(と仮定しImageViewのは財産であり、cornerRadiusを所望の角の半径です):

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

すでにself.CGImageを持っているので、あなたはUIImageViewを作成するために、これを行うことができます:

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

imageViewをサブビューとして追加した後、必ずそれをリリースしてください。

1
あなたは、ビューのレイヤーに「cornerRadius」を追加し、YESにmasksToBoundsを設定していない理由を
profileImageView.layer.cornerRadius = profileImageView.frame.size.height/2; 
profileImageView.clipsToBounds = YES; 
+1

裸のコードをポストするのではなく、ソリューションが最良の理由、または問題の解決方法に関する説明を追加すると役立ちます。 –

関連する問題