1、如何高性能的給UIImageView加個(gè)圓角

? 不好的解決方案

? 使用下面的方式會強(qiáng)制Core Animation提前渲染屏幕的離屏繪制, 而離屏繪制就會給性能帶來負(fù)面影響,會有卡頓的現(xiàn)象出現(xiàn)self.view.layer.cornerRadius = 5;

? self.view.layer.masksToBounds = YES;

?

? 正確的解決方案:使用繪圖技術(shù)

- (UIImage *)circleImage

{

? ? // NO代表透明

? ? UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);

? ? // 獲得上下文

? ? CGContextRef ctx = UIGraphicsGetCurrentContext();

? ? // 添加一個(gè)圓

? ? CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);

? ? CGContextAddEllipseInRect(ctx, rect);

? ? // 裁剪

? ? CGContextClip(ctx);

? ? // 將圖片畫上去

? ? [self drawInRect:rect];

? ? UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

? ? // 關(guān)閉上下文

? ? UIGraphicsEndImageContext();

? ? return image;

}

? 還有一種方案:使用了貝塞爾曲線"切割"個(gè)這個(gè)圖片, 給UIImageView 添加了的圓角,其實(shí)也是通過繪圖技術(shù)來實(shí)現(xiàn)的

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];

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容