首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用圆形遮罩正方形图像并在图像周围放置黑色边框?

如何使用圆形遮罩正方形图像并在图像周围放置黑色边框?
EN

Stack Overflow用户
提问于 2012-10-26 13:40:34
回答 1查看 5K关注 0票数 4

我有一个40x40的正方形图像,我想通过裁剪使它变圆,但也要在图像周围放置一个5像素的黑色边框。

我有以下几个掩蔽正方形的图像,所以它现在是圆形的

代码语言:javascript
运行
复制
 UIImage *image = self.imageView.image;
        CGSize imageSize = image.size;
        CGRect imageRect = CGRectMake(0, 0, imageSize.width, imageSize.height);

        UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
        // Create the clipping path and add it
        UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:imageRect];
        [path addClip];


        [image drawInRect:imageRect];
        UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        self.imageView.image = roundedImage;

但现在我还需要在它周围添加一个圆形边框。我是否需要一个新的路径,或者我是否可以直接添加到上面代码中的路径?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-26 14:25:18

在您的代码中添加以下三行(具有所需的颜色和笔触宽度):

代码语言:javascript
运行
复制
CGContextSetStrokeColorWithColor(ctx, [[UIColor greenColor] CGColor]);
[path setLineWidth:50.0f];
[path stroke];

所以它变成了:

代码语言:javascript
运行
复制
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
// Create the clipping path and add it
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:imageRect];
[path addClip];
[image drawInRect:imageRect];

CGContextSetStrokeColorWithColor(ctx, [[UIColor greenColor] CGColor]);
[path setLineWidth:50.0f];
[path stroke];

UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

self.imageView.image = roundedImage;
票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13081356

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档