1. 程式人生 > >設定圓角圖片的兩種方法

設定圓角圖片的兩種方法

在專案需求中有時會要求將圖片設定成圓形的,尤其是頭像。將圖片設定成圓形暫時知道有兩種方法:1.設定圓角  2.將圖片進行裁剪。

方法1:設定圓角

程式碼:

imagV.layer.cornerRadius = 5;   // (設定為正方形圖片的邊長的一半)

imagV.layer.masksToBounds = YES;

在iOS8之前此方法設定的圖片如果放在單元格中,且圖片較多的情況下就會產生卡頓,是不被建議使用的方法。可iOS8之後蘋果對其進行了處理(實際就是使用了之前別人處理這樣卡頓的一個三方庫)。所以iOS8之後就可以放心使用了。 方法2:裁剪圖片 程式碼:

    [imagV

sd_setImageWithURL:[NSURLURLWithString:@"http://h.hiphotos.baidu.com/image/h%3D200/sign=b02f687c8a26cffc762ab8b289004a7d/42166d224f4a20a44149222298529822730ed0fb.jpg"] placeholderImage:niloptions:SDWebImageCacheMemoryOnlycompleted:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

UIGraphicsBeginImageContextWithOptions

(image.size, NO, 0);

UIBezierPath * path = [UIBezierPathbezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.height)];

        [path addClip];

        [image drawAtPoint:CGPointZero];

        image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

        imagV.

image = image;

    }];

在iOS8之後,方法1不會出現卡頓的情況,本人更傾向於使用方法1,畢竟程式碼簡單大笑