I made a method to resize an image. When it resizes an image in images array, it become too much memories(e.g. 200MB~300MB) especially when an image is a High-Quality image(iPhone6/6s). My Project is in ARC. If I want to make it, how do I have to code it? Please teach me how to "immediately" or "forced" release "trimmedImage" instance.
I find this by Instruments(CGImage eats much memory). I tried to use @autoreleasepool, trimmedImage = nil; and [trimmedImage release];, but they also work bad(not released trimmedImage memory).
+ (void)cropImages: (NSArray *)images {
for (__weak UIImage *image in images) {
UIImage *resizedImage = [self cropRectImage:image];
}
}
+ (UIImage *)cropRectImage: (UIImage *)image {
float w = image.size.width;
float h = image.size.height;
CGRect rect;
if (h <= w) {
float x = w / 2 - h / 2;
float y = 0;
rect = CGRectMake(x, y, h, h);
}else {
float x = 0;
float y = h / 2 - w / 2;
rect = CGRectMake(x, y, w, w);
}
CGImageRef cgImage = CGImageCreateWithImageInRect(image.CGImage, rect);
UIImage *trimmedImage = [UIImage imageWithCGImage:cgImage];
CGSize newSize = CGSizeMake(320, 320);
UIGraphicsBeginImageContext(newSize);
UIImage *resizedImage = nil;
[trimmedImage drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
// TODO: ここでAutoReleaseされるはずだが... (http://vladimir.zardina.org/2010/05/resizing-uiimage-objects/)
resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
image = nil;
cgImage = nil;
return resizedImage;
}