学习链接:https://www.jianshu.com/p/8f508f4ffd2c
直接上代码吧
//将image图片转化为符合要求的图片
- (UIImage *)transformImage:(UIImage *)image WithTargetRect:(CGRect) targetRect {
//将image改为50 * 50pt大小的
CGSize smallImageSize = CGSizeMake(50.0f, 50.0f);
//创建一个目标大小的画板
UIGraphicsBeginImageContextWithOptions(smallImageSize, NO, [[UIScreen mainScreen] scale]);
//将需要转换的图片画在上面
[image drawInRect:CGRectMake(0, 0, smallImageSize.width, smallImageSize.height)];
//保存下来
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
//结束画板
UIGraphicsEndImageContext();
//创建一个纯色图片
UIColor *backgroudColor =[UIColor whiteColor];
UIGraphicsBeginImageContextWithOptions(targetRect.size, NO, [[UIScreen mainScreen] scale]);
CGContextRef context = UIGraphicsGetCurrentContext();
//设置圆角
float radius = 8.0f;
CGContextAddPath(context, [UIBezierPath bezierPathWithRoundedRect:targetRect cornerRadius:radius].CGPath);
CGContextClip(context);
CGContextSetFillColorWithColor(context, [backgroudColor CGColor]);//填充
CGContextFillRect(context, targetRect);
//将smallImage画到纯色背景上
CGFloat margin = 4.0f;
[smallImage drawAtPoint:CGPointMake(margin, margin)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}