-
不好的解決方案
-
使用下面的方式會
強制Core Animation提前渲染屏幕的離屏繪制, 而離屏繪制就會給性能帶來負面影響
,會有卡頓的現象出現self.view.layer.cornerRadius = 5; self.view.layer.masksToBounds = YES;
-
-
正確的解決方案:使用繪圖技術
- (UIImage *)circleImage { // NO代表透明 UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0); // 獲得上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); // 添加一個圓 CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height); CGContextAddEllipseInRect(ctx, rect); // 裁剪 CGContextClip(ctx); // 將圖片畫上去 [self drawInRect:rect]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); // 關閉上下文 UIGraphicsEndImageContext(); return image; }
- 還有一種方案:使用了貝塞爾曲線"切割"個這個圖片, 給UIImageView 添加了的圓角,其實也是通過繪圖技術來實現的
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; imageView.center = CGPointMake(200, 300); UIImage *anotherImage = [UIImage imageNamed:@"image"]; UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0); [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:50] addClip]; [anotherImage drawInRect:imageView.bounds]; imageView.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); [self.view addSubview:imageView];
下面這篇博客寫的非常不錯,推薦看看:對於圖片的處理
http://www.jianshu.com/p/4e22c6ac114d