我开发了从矩形 UIImage 到圆形的方法。问题是,如果连续对 10 个不同的图像执行该操作,则会降低性能。图片分辨率为 120x120
- (UIImage *)roundedImage:(UIImage*)anOriginalImage radius:(CGFloat)aRadius
{
UIImage *result = nil;
if (anOriginalImage != nil) {
UIGraphicsBeginImageContextWithOptions(anOriginalImage.size, NO, 0);
[[UIBezierPath bezierPathWithRoundedRect:(CGRect){CGPointZero, anOriginalImage.size}
cornerRadius:aRadius] addClip];
[anOriginalImage drawInRect:(CGRect){CGPointZero, anOriginalImage.size}];
result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return result;
}
如何解决这个问题?
最佳答案
导入 QuartzCore 框架以访问 UIView 或 UIImageView 的 .cornerRadius 属性。
#import <QuartzCore/QuartzCore.h>
还可以手动将其添加到项目的 Frameworks 文件夹中。
将此方法添加到您的 View Controller 或任何您需要的地方:
-(void)setRoundedView:(UIImageView *)roundedView toDiameter:(float)newSize;
{
CGPoint saveCenter = roundedView.center;
CGRect newFrame = CGRectMake(roundedView.frame.origin.x, roundedView.frame.origin.y, newSize, newSize);
roundedView.frame = newFrame;
roundedView.layer.cornerRadius = newSize / 2.0;
roundedView.center = saveCenter;
}
要使用它,只需向其传递一个 UIImageView 和一个直径。此示例假设您有一个名为“circ”的 UIImageView 作为 subview 添加到您的 View 中。它应该设置一个背景颜色,以便您可以看到它。
[self setRoundedView:circ toDiameter:100.0];
这仅处理 UIImageViews,但您可以将其推广到任何 UIView。
关于objective-c - 尝试制作圆角 UIImage 时的性能问题(使用 mask ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21650064/