不好的解決方案:
使用下面的方式會(huì)強(qiáng)制Core Animation提前渲染屏幕的離屏繪制, 而離屏繪制就會(huì)給性能帶來負(fù)面影響,會(huì)有卡頓的現(xiàn)象出現(xiàn)。
self.view.layer.cornerRadius =5.0f;
self.view.layer.masksToBounds =YES;
正確的解決方案:使用繪圖技術(shù)
- (UIImage*)circleImage {
UIGraphicsBeginImageContextWithOptions(self.size,NO,0.0);?// NO代表透明
CGContextRefctx =UIGraphicsGetCurrentContext();?// 獲得上下文
CGRectrect =CGRectMake(0,0,self.size.width,self.size.height);CGContextAddEllipseInRect(ctx, rect);
CGContextClip(ctx); ?// 添加一個(gè)圓?裁剪
// 將圖片畫上去
[selfdrawInRect:rect];UIImage*image =UIGraphicsGetImageFromCurrentImageContext();
// 關(guān)閉上下文
UIGraphicsEndImageContext();returnimage;}
還有一種方案:使用了貝塞爾曲線"切割"個(gè)這個(gè)圖片, 給UIImageView 添加了的圓角阿宅,其實(shí)也是通過繪圖技術(shù)來實(shí)現(xiàn)的接谨。
UIImageView*imageView = [[UIImageViewalloc] initWithFrame:CGRectMake(0,0,100,100)];imageView.center =CGPointMake(200,300);
UIImage*anotherImage = [UIImageimageNamed:@"image"];
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size,NO,1.0);[[UIBezierPathbezierPathWithRoundedRect:imageView.bounds cornerRadius:50] addClip];
[anotherImage drawInRect:imageView.bounds];
imageView.image =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.view addSubview:imageView];