CGImageRef imgRef = [image CGImage]; 通過此種方式的得到的CGImageRef不能利用CGImageRelease釋放,因為你不擁有它所以不用釋放
在ios中特定形狀剪裁圖片的實現(注意要釋放記憶體,不然即使用ARC也會出現記憶體洩露)
- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)mask { CGImageRef imgRef = [image CGImage]; CGImageRef maskRef = [mask CGImage]; CGImageRef actualMask = CGImageMaskCreate(CGImageGetWidth(maskRef), CGImageGetHeight(maskRef), CGImageGetBitsPerComponent(maskRef), CGImageGetBitsPerPixel(maskRef), CGImageGetBytesPerRow(maskRef), CGImageGetDataProvider(maskRef), NULL, NO); CGImageRef masked = CGImageCreateWithMask(imgRef, actualMask); UIImage *newImage = [UIImage imageWithCGImage:masked]; CGImageRelease(actualMask); CGImageRelease(masked); return newImage; }