这种裁剪只能裁剪png格式图片,请注意。个人能力不足,希望大家一起思维碰撞。
#import
@interface UIImage (QKCornerRadius)
+(UIImage*)UIBezierPathClip:(UIImage*)img cornerRadius:(CGFloat)c;
+(UIImage*)CGContextClip:(UIImage*)img cornerRadius:(CGFloat)c;
@end
//
//Created by郑乾坤on 2017/5/24.
//Copyright © 2017年ZW. All rights reserved.
//
#import "UIImage+QKCornerRadius.h"
@implementation UIImage (QKCornerRadius)
// CGContext裁剪
+(UIImage*)CGContextClip:(UIImage*)img cornerRadius:(CGFloat)c {
intw = img.size.width* img.scale;
inth = img.size.height* img.scale;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(w, h),false,1.0);
CGContextRefcontext =UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context,0, c);
CGContextAddArcToPoint(context,0,0, c,0, c);
CGContextAddLineToPoint(context, w-c,0);
CGContextAddArcToPoint(context, w,0, w, c, c);
CGContextAddLineToPoint(context, w, h-c);
CGContextAddArcToPoint(context, w, h, w-c, h, c);
CGContextAddLineToPoint(context, c, h);
CGContextAddArcToPoint(context,0, h,0, h-c, c);
CGContextAddLineToPoint(context,0, c);
CGContextClosePath(context);
CGContextClip(context);//先裁剪context,再画图,就会在裁剪后的path中画
[imgdrawInRect:CGRectMake(0,0, w, h)];//画图
CGContextDrawPath(context,kCGPathFill);
UIImage*ret =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
returnret;
}
// UIBezierPath裁剪
+(UIImage*)UIBezierPathClip:(UIImage*)img cornerRadius:(CGFloat)c {
intw = img.size.width* img.scale;
inth = img.size.height* img.scale;
CGRectrect =CGRectMake(0,0, w, h);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(w, h),false,1.0);
[[UIBezierPathbezierPathWithRoundedRect:rectcornerRadius:c]addClip];
[imgdrawInRect:rect];
UIImage*ret =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
returnret;
}
@end
网友评论