简体   繁体   中英

iOS CIFilter of RAW Image Memory Issue (Objective-C)

i am applying a CIFilter to a RAW image however I am facing some memory problems when I try to display it in an UIImageView.
I apply the CIFilter by using the following code:

CIFilter *rawshadow = [CIFilter filterWithImageURL:initialImageUrl options:@{kCIInputBoostShadowAmountKey: [NSNumber numberWithFloat:self.shadowsSlider.value],kCIInputScaleFactorKey: [NSNumber numberWithFloat:1]}];
currentImage = rawshadow.outputImage;

And then I update the UIImageView by using the following:

-(void) UpdatePreview{
    CIContext *context = [CIContext contextWithOptions:@{kCIContextCacheIntermediates: [NSNumber numberWithBool:NO], kCIContextPriorityRequestLow:[NSNumber numberWithBool:YES]}];
    CGImageRef cgImage = [context createCGImage:currentImage fromRect:[currentImage extent]];
    UIImage* img = [UIImage imageWithCGImage:cgImage];
    CGImageRelease(cgImage);
    [self.mainImageView setImage:img];
}

After running UpdatePreview the RAM spikes up to 2GB and crashes. The raw image I am using is 6000x4000 and is around 24MB.
Is it too big or am I missing something?
Thanks in advance for your help.

Even if you were not using CIFilter, it is always wrong to assign to an image view an image larger than needed for display. Your image view is not 6000x4000, so your image should not be. And in fact, your image is not 6000x4000: it is 12000x8000, or even 18000x12000, because you are converting from a CGImage to a UIImage incorrectly (you are using an implicit scale of 1, rather than setting the scale to your screen's resolution). No wonder you're crashing.

Process the original image at the needed size, or reduce the image size as part of the filter chain, so that the output image is the size actually needed for display in your interface. (And there is no need to pass through a CGImage on the way to a UIImage; you can render directly from a CIImage to a UIImage.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM