版权声明:本文为博主原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1337936
在项目需求中有时会要求将图片设置成圆形的,尤其是头像。将图片设置成圆形暂时知道有两种方法:1.设置圆角 2.将图片进行裁剪。
方法1:设置圆角
代码:
imagV.layer.cornerRadius = 5; // (设置为正方形图片的边长的一半)
imagV.layer.masksToBounds = YES;
在iOS8之前此方法设置的图片如果放在单元格中,且图片较多的情况下就会产生卡顿,是不被建议使用的方法。可iOS8之后苹果对其进行了处理(实际就是使用了之前别人处理这样卡顿的一个三方库)。所以iOS8之后就可以放心使用了。
方法2:裁剪图片
代码:
[imagV sd_setImageWithURL:NSURL URLWithString:@"http://h.hiphotos.baidu.com/image/h%3D200/sign=b02f687c8a26cffc762ab8b289004a7d/42166d224f4a20a44149222298529822730ed0fb.jpg" placeholderImage:nil options:SDWebImageCacheMemoryOnly completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
UIBezierPath * path = UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.height);
path addClip;
image drawAtPoint:CGPointZero;
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imagV.image = image;
}];
在iOS8之后,方法1不会出现卡顿的情况,本人更倾向于使用方法1,毕竟代码简单