[摘要]本文是對UITableView性能-圓角圖片的講解,對學習IOS蘋果軟件開發有所幫助,與大家分享。
圓角圖片因為GPU渲染會影響性能
參考:http://www.cocoachina.com/ios/20150803/12873.html
http://blog.sina.com.cn/s/blog_671d2e4f0101cxpl.html
http://www.cnblogs.com/thefeelingofsimple/archive/2013/02/20/2918547.html
instruments用的不多,我直接循環了幾個圓角,3種方法試了下
image.layer.cornerRadius=8;
image.layer.masksToBounds=YES;
上面效率是最低的,
另外的重寫draw方法,和mask圖與原圖覆蓋生成新圖,效率感覺差不多,等後面熟悉了instruments在測試下,
重寫drawRect,(-_-,要弄個新的屬性才會走這裡,本菜鳥還不懂啊,先弄了個UIImageView用它自己的image走都不走這裡。。。圖形這方面設計太少了)
//注意背景色要和cell的背景色設置一致哦,不然-0-
-(void)drawRect:(CGRect)rect{
CGRectbounds=self.bounds;
[[UIColorwhiteColor]set];
UIRectFill(bounds);
[[UIBezierPathbezierPathWithRoundedRect:rectcornerRadius:8.0]addClip];
[self.imagedrawInRect:bounds];
}
-(void)setImage:(UIImage*)image{
_image=image;
//需要這一步,需要刷新,不然顯示會出問題
[selfsetNeedsDisplay];
}
-(void)dealloc{
self.image=nil;
}
mask圖與原圖覆蓋生成新圖
-(UIImage*)imageByComposingImage:(UIImage*)imagewithMaskImage:(UIImage*)maskImage{
CGImageRefmaskImageRef=maskImage.CGImage;
CGImageRefmaskRef=CGImageMaskCreate(CGImageGetWidth(maskImageRef),
CGImageGetHeight(maskImageRef),
CGImageGetBitsPerComponent(maskImageRef),
CGImageGetBitsPerPixel(maskImageRef),
CGImageGetBytesPerRow(maskImageRef),
CGImageGetDataProvider(maskImageRef),NULL,false);
CGImageRefnewImageRef=CGImageCreateWithMask(image.CGImage,maskRef);
CGImageRelease(maskRef);
UIImage*newImage=[UIImageimageWithCGImage:newImageRef];
CGImageRelease(newImageRef);
returnnewImage;
}
補充:mask圖與原圖覆蓋生成新圖可以自定義任意形狀的圖哦
測試demo鏈接:http://pan.baidu.com/s/1qWCDprY