圆角图片因为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;
image.layer.masksToBounds = YES;
上面效率是最低的,
另外的重写draw方法,和mask图与原图覆盖生成新图,效率感觉差不多,等后面熟悉了instruments在测试下,
重写drawRect,(-_-,要弄个新的属性才会走这里,本菜鸟还不懂啊,先弄了个UIImageView用它自己的image走都不走这里。。。图形这方面设计太少了)
//注意背景色要和cell的背景色设置一致哦,不然-0-
- (void)drawRect:(CGRect)rect {
CGRect bounds = self.bounds;
[[UIColor whiteColor]set];
UIRectFill(bounds);
[[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:8.0] addClip];
[self.image drawInRect:bounds];
}
- (void)setImage:(UIImage *)image {
_image = image;
//需要这一步,需要刷新,不然显示会出问题
[self setNeedsDisplay];
}
- (void)dealloc {
self.image = nil;
}
- (void)drawRect:(CGRect)rect {
CGRect bounds = self.bounds;
[[UIColor whiteColor]set];
UIRectFill(bounds);
[[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:8.0] addClip];
[self.image drawInRect:bounds];
}
- (void)setImage:(UIImage *)image {
_image = image;
//需要这一步,需要刷新,不然显示会出问题
[self setNeedsDisplay];
}
- (void)dealloc {
self.image = nil;
}
mask图与原图覆盖生成新图
- (UIImage *)imageByComposingImage:(UIImage *)image withMaskImage:(UIImage *)maskImage {
CGImageRef maskImageRef = maskImage.CGImage;
CGImageRef maskRef = CGImageMaskCreate(CGImageGetWidth(maskImageRef),
CGImageGetHeight(maskImageRef),
CGImageGetBitsPerComponent(maskImageRef),
CGImageGetBitsPerPixel(maskImageRef),
CGImageGetBytesPerRow(maskImageRef),
CGImageGetDataProvider(maskImageRef), NULL, false);
CGImageRef newImageRef = CGImageCreateWithMask(image.CGImage, maskRef);
CGImageRelease(maskRef);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
CGImageRelease(newImageRef);
return newImage;
}
CGImageRef maskImageRef = maskImage.CGImage;
CGImageRef maskRef = CGImageMaskCreate(CGImageGetWidth(maskImageRef),
CGImageGetHeight(maskImageRef),
CGImageGetBitsPerComponent(maskImageRef),
CGImageGetBitsPerPixel(maskImageRef),
CGImageGetBytesPerRow(maskImageRef),
CGImageGetDataProvider(maskImageRef), NULL, false);
CGImageRef newImageRef = CGImageCreateWithMask(image.CGImage, maskRef);
CGImageRelease(maskRef);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
CGImageRelease(newImageRef);
return newImage;
}
补充:mask图与原图覆盖生成新图可以自定义任意形状的图哦
测试demo链接:http://pan.baidu.com/s/1qWCDprY