iOS 中给图片加圆角的最好方法是什么?
加圆角有很多方法,比如:
我认为最好的方法是
- 在CALayer中设置cornerRadius属性, 但是很慢, 尤其是图片作为头像显示在UITableView中.
- 用一张头像的placeholder的图, 但是要多读取一张图.
- 直接将图片裁剪并加上透明圆角. 但是每张图都要处理.
我认为最好的方法是
4. drawRect中使用UIBezierPath画一个圆角的path, 然后clip. 但是如果把UIView的opaque设置成YES, 就会出现黑色的边. 不知道有什么办法可以避免这个问题又能得到最高性能.
第一是直接设置参数:
view.layer.shouldRasterize = YES;
view.layer.rasterizationScale = view.window.screen.scale; // or [UIScreen mainScreen]
原文链接:http://stackoverflow.com/questions/11049016/cliptobounds-and-maskstobounds-performance-issue
第二是你说的UIBezierPath的方法,我觉得也挺好的呀:
// Get your image somehow
UIImage *image = [UIImage imageNamed:@"image.jpg"];
// Begin a new image that will be the new image with the rounded corners
// (here with the size of an UIImageView)
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);
// Add a clip before drawing anything, in the shape of an rounded rect
[[UIBezierPath bezierPathWithRoundedRect:imageView.bounds
cornerRadius:10.0] addClip];
// Draw your image
[image drawInRect:imageView.bounds];
// Get the image, here setting the UIImageView image
imageView.image = UIGraphicsGetImageFromCurrentImageContext();
// Lets forget about that we were drawing
UIGraphicsEndImageContext();
原文链接:http://stackoverflow.com/questions/17593524/using-cornerradius-on-a-uiimageview-in-a-uitableviewcell