[UIImage imageWithCGImage:CGImageCreateWithImageInRect(<#CGImageRef _Nullable image#>, <#CGRect rect#>)]
注意这里的rect是相对于 所给图片的真实像素而言的,比如图片的像素是100x100,如果要得到宽度为这个图片宽度一半的新图片,rect应该写成(0, 0, 50, 100)
-
// get part of the image - - (UIImage *)getPartOfImage:(UIImage *)img rect:(CGRect)partRect
- {
- CGImageRef imageRef = img.CGImage;
- CGImageRef imagePartRef = CGImageCreateWithImageInRect(imageRef, partRect);
- UIImage *retImg = [UIImage imageWithCGImage:imagePartRef];
- CGImageRelease(imagePartRef);
- return retImg;
- }
注意下:
这个方法中写了释放CGImageRef imagePartRef的代码CGImageRelease(imagePartRef)
因为用imagePartRef生成新图片之后就没用了,需要释放掉