将 UIImage 制作成圆形
我一直在尝试将 UIImage 屏蔽成一个圆圈。 我现在使用的代码在其他答案中很流行,但是虽然我确实得到了一个圆圈,但它的边缘非常锯齿状且不平滑。 有人可以帮忙吗?我能得到一个完美、光滑的圆圈吗?
我用来创建面具的代码是:
(UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {
CGImageRef imageNoAlpha = image.CGImage;
CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
CGFloat width = CGImageGetWidth(imageNoAlpha);
CGFloat height = CGImageGetWidth(imageNoAlpha);
CGContextRef ctxWithAlpha = CGBitmapContextCreate(nil, width, height, 8, 4*width, cs, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(ctxWithAlpha, CGRectMake(0, 0, width, height), imageNoAlpha);
CGImageRef imageWithAlpha = CGBitmapContextCreateImage(ctxWithAlpha);
CGImageRef maskRef = maskImage.CGImage;
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), NULL, false);
CGImageRef masked = CGImageCreateWithMask(imageWithAlpha, mask);
UIImage* retImage= [UIImage imageWithCGImage:masked];
UIImage* retImageFixed = [retImage transparentBorderImage:1];
CGImageRelease(mask);
CGImageRelease(masked);
CGImageRelease(imageWithAlpha);
CGContextRelease(ctxWithAlpha);
CGColorSpaceRelease(cs);
return retImageFixed;
提前致谢......
I have been trying to mask a UIImage into a circle.
I'm using now the code that has been popular on other answers here, but although I do get a circle its edges are very jagged and not smooth.
Anyone can help? I do I get a perfect, smooth circle?
The code I'm using to create the mask is:
(UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {
CGImageRef imageNoAlpha = image.CGImage;
CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
CGFloat width = CGImageGetWidth(imageNoAlpha);
CGFloat height = CGImageGetWidth(imageNoAlpha);
CGContextRef ctxWithAlpha = CGBitmapContextCreate(nil, width, height, 8, 4*width, cs, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(ctxWithAlpha, CGRectMake(0, 0, width, height), imageNoAlpha);
CGImageRef imageWithAlpha = CGBitmapContextCreateImage(ctxWithAlpha);
CGImageRef maskRef = maskImage.CGImage;
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), NULL, false);
CGImageRef masked = CGImageCreateWithMask(imageWithAlpha, mask);
UIImage* retImage= [UIImage imageWithCGImage:masked];
UIImage* retImageFixed = [retImage transparentBorderImage:1];
CGImageRelease(mask);
CGImageRelease(masked);
CGImageRelease(imageWithAlpha);
CGContextRelease(ctxWithAlpha);
CGColorSpaceRelease(cs);
return retImageFixed;
Thanks in advance...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
尝试这个代码
,显示像 ios 7 圆形图像一样的图像,谢谢
try this code
this show image like ios 7 circle image thanks
这可能会帮助您,将角半径作为要显示图像的参考视图的一半,或者您可以设置自定义矩形,
例如。就我而言,我想在 "imageView" 上显示图像,因此本例中的角半径为 imageView.frame.size.width/2
This might help you, pass corner radius as half of the reference view where you want to display the image or you can set a custom rect,
eg. In my case I want to display the image on a "imageView", so the corner radius in this case would be imageView.frame.size.width/2
抱歉格式很丑。
aToz 答案的更好版本。
Swift 中同样的事情:
Sorry for ugly formatting.
Better version of aToz's answer.
The same thing in Swift:
aToz 和 fnc12 答案的更好版本(使用 Swift):
用法:
A better version of aToz's and fnc12's answers (in Swift):
Usage:
感觉我应该把我的帽子扔进拳击场。有效、紧凑的 Swift 5 解决方案:
Felt like I should throw my hat into the ring. An effective, compact Swift 5 solution:
从 UIImageView 获取帮助怎么样
What about getting help from UIImageView
如果您使用 Swift,则可以导入“AlamofireImage”,并使用其函数之一。对于带圆圈的图像:
If you're using Swift, you can import 'AlamofireImage', and use one of its funcs. For circled image:
如果您使用
UIImageView
并且希望在保持圆角的同时对图像的大小进行动画处理,则可以按如下方式应用转换:Objective-C
Swift
注意:这还将保留任何自动布局
约束
。If you are using an
UIImageView
and you are looking to animate the size of the image while maintaining the rounded corners, you can apply a transformation as follows:Objective-C
Swift
NOTE: This will also maintain any AutoLayout
constraints
.我用 UIBezierPath 解决了我的问题
I resolve my problem with UIBezierPath
试试这个
注意:确保该视图的高度和宽度相同。
try this
Note:Make Sure height and width will be same of that view.