一。CGImageRef方法剪切图片
+(UIImage*)clipImage:(UIImage *)image Frame:(CGRect)frame{
CGImageRef imgRef = CGImageCreateWithImageInRect(image.CGImage, frame);
UIImage * newImage = [UIImage imageWithCGImage:imgRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
//默认图片正立
return newImage;
}
二。使用UIGraphics
+(UIImage*)screenShopWithRect:(CGRect)rect InView:(UIView*)currentView{
//1 开启图形上下文
UIGraphicsBeginImageContextWithOptions(rect.size, YES, 0.0);
//设置截屏区域
UIBezierPath * shotPath = [UIBezierPath bezierPathWithRect:rect];
//剪切目标区域
[shotPath addClip];
//如果是图片 则画进图形上下文
//[image drawInRect:frame]
//frame为图形上下文中的某一个区域
//如果是视图,则直接渲染进图形上下文
[currentView.layer renderInContext:UIGraphicsGetCurrentContext()];
//使用图片保存当前图形上下文
UIImage * shotImage = UIGraphicsGetImageFromCurrentImageContext();
//结束图形上下文
UIGraphicsEndImageContext();
return shotImage;
}