1、剪切图片
/**
* 从图片中按指定的位置大小截取图片的一部分
*
* @param image UIImage image 原始的图片
* @param rect CGRect rect 要截取的区域
*
* @return UIImage
*/
- (UIImage *)ct_imageFromImage:(UIImage *)image inRect:(CGRect)rect{
//把像 素rect 转化为 点rect(如无转化则按原图像素取部分图片)
CGFloat scale = [UIScreen mainScreen].scale;
CGFloat x= rect.origin.x*scale,y=rect.origin.y*scale,w=rect.size.width*scale,h=rect.size.height*scale;
CGRect dianRect = CGRectMake(x, y, w, h);
//截取部分图片并生成新图片
CGImageRef sourceImageRef = [image CGImage];
CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, dianRect);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
#warning 不release newImage,会造成内存泄漏
CGImageRelease(newImageRef);
return newImage;
}
网友评论