沿着UIImage圈添加白色边框

时间:2012-12-13 21:16:10

标签: iphone objective-c ios ipad

我使用掩码生成了一个圆形UIImage,如下所示(其中masked_circle是黑色圆圈):

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白色边框,我该怎么办?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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);