2012-12-13 98 views
1

我已生成使用掩模的圓的UIImage如下(其中masked_circle是一個黑圈):添加白色邊框沿着圓圈的UIImage

CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 

    UIImage *maskImage = [UIImage imageNamed:@"masked_circle.png"]; 
    CGImageRef maskImageRef = [maskImage CGImage]; 

    // create a bitmap graphics context the size of the image 
    CGContextRef mainViewContentContext = CGBitmapContextCreate (NULL, maskImage.size.width, maskImage.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast); 

    CGFloat ratio = 0; 
    ratio = maskImage.size.width/ self.image_.size.width; 

    if(ratio * self.image_.size.height < maskImage.size.height) { 
     ratio = maskImage.size.height/ self.image_.size.height; 
    } 

    CGRect rect1 = {{0, 0}, {maskImage.size.width, maskImage.size.height}}; 
    CGRect rect2 = {{-((self.image_.size.width*ratio)-maskImage.size.width)/2 , -((self.image_.size.height*ratio)-maskImage.size.height)/2}, {self.image_.size.width*ratio, self.image_.size.height*ratio}}; 

    CGContextClipToMask(mainViewContentContext, rect1, maskImageRef); 
    CGContextDrawImage(mainViewContentContext, rect2, self.image_.CGImage); 

    CGImageRef newImage = CGBitmapContextCreateImage(mainViewContentContext); 
    CGContextRelease(mainViewContentContext); 

    UIImage *theImage = [UIImage imageWithCGImage:newImage]; 

現在,在這之後,我想周圍添加1px的白色邊框圓形圖像,我該怎麼辦?

+0

UIImage會在UIImageView中,或者你想要在UIImage中繪製實際的邊框嗎? – Eric

+0

想要在UIImage本身中繪製實際的邊框 – adit

回答

2

這裏有一些代碼圍繞一個帶有圓角半徑的框架繪製邊框。使該半徑等於一半大小,並且你有一個圓圈!

CGFloat strokeWidth =1.0; 
CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSetLineWidth(context, strokeWidth); 
CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor); 

CGFloat radius = 7.0; 
CGRect rrect = self.bounds; 
rrect.size.width = rrect.size.width - strokeWidth*2; 
rrect.size.height = rrect.size.height - strokeWidth*2; 
rrect.origin.x = rrect.origin.x + (strokeWidth/2); 
rrect.origin.y = rrect.origin.y + (strokeWidth/2); 
CGFloat width = CGRectGetWidth(rrect); 
CGFloat height = CGRectGetHeight(rrect); 

if (radius > width/2.0) 
    radius = width/2.0; 
if (radius > height/2.0) 
    radius = height/2.0; 

CGFloat minx = CGRectGetMinX(rrect); 
CGFloat midx = CGRectGetMidX(rrect); 
CGFloat maxx = CGRectGetMaxX(rrect); 
CGFloat miny = CGRectGetMinY(rrect); 
CGFloat midy = CGRectGetMidY(rrect); 
CGFloat maxy = CGRectGetMaxY(rrect); 
CGContextMoveToPoint(context, minx, midy); 
CGContextAddArcToPoint(context, minx, miny, midx, miny, radius); 
CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius); 
CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius); 
CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius); 
CGContextClosePath(context); 
CGContextDrawPath(context, kCGPathFillStroke); 
5

這裏有一個更簡單的解決方案,把任何看法,但具體一個UIImageView成圈具有不同大小和顏色的邊框。這當然假設你正在處理像圖標這樣的方形圖像。

#import <UIKit/UIKit.h> 

@interface UIView (Shapes) 
- (void)makeCircle; 
- (void)makeCircleWithBorderColor:(UIColor *) color Width:(CGFloat) width; 
@end 


@implementation UIView (Shapes) 

- (void)makeCircle { 
    CALayer *lyr = self.layer; 
    lyr.masksToBounds = YES; 
    lyr.cornerRadius = self.bounds.size.width/2; // assumes image is a square 
} 

- (void)makeCircleWithBorderColor:(UIColor *) color Width:(CGFloat) width { 
    [self makeCircle]; 
    CALayer *lyr = self.layer; 
    lyr.borderWidth = width; 
    lyr.borderColor = [color CGColor]; 
} 
@end 
+0

爲了簡單起見,您不需要創建* lyr就可以直接使用self.layer和+1。 –