Memory issue in using UIImagePNGRepresentation
Asked Answered
W

3

3

I found this module to be troublesome. I import more than 100 images from Photolibrary, save them in documents directory with a different name. As expected I had a memory issue in the unusual place. It seems UIImagePNGRepresenation is caching files. So when I run the below process for 300+ images, I see "Overall bytes" in the range of 3.00 GB and crashes due to Memory (tested in allocations tool). I have pasted the code below. Is there any alternative for this code

-(void)something
{
   NSData *data=nil;
   for (int i=0; i<numberOfImages; i++) {
    
    @autoreleasepool {
        
        UIImage *image=[UIImage imageNamed:[NSString stringWithFormat:@"image%d.png",i]];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        
        NSString *documentsDirectory = [[paths objectAtIndex:0] stringByAppendingString:@"directoryname"];
        
        NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"image%d.png",i]];
        
        //convert image into .png format
        data=UIImagePNGRepresentation(image);
        [data writeToURL:[NSURL URLWithString:fullPath] atomically:NO];
      }
   }
   data=nil;
}
Woodbridge answered 30/1, 2012 at 10:34 Comment(0)
W
1

I mailed this issue to Apple and they asked me to introduce sleep cycles between every allocation. Add sleep before allocation.

Woodbridge answered 2/2, 2012 at 11:32 Comment(1)
I also tried this but not helpful..Please provide other way to solve this...Thanks in advanceSodium
W
2

I solve this issue by sending a 4 channels image (RGBA or RGBX) instead of a 3 channels image (RGB).

You can check if there's any chance to change parameters of your image.

Whyalla answered 22/12, 2016 at 2:9 Comment(0)
M
1

The caching is coming from [UIImage imageNamed:], not UIImagePNGRepresentation(). Do this instead:

NSString *imageName = [NSString stringWithFormat:@"image%d.png", i];
NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageName];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];

...
Michaelamichaele answered 30/1, 2012 at 10:39 Comment(5)
No. It did not work. I tried with [UIImage imageWithContentsOfFile:], [[UIImage alloc]initWithContentsOfFile:]. The issue is during conversion of UIImage to NSData. I checked the above some 15 timesWoodbridge
Ok, well seen as you're not using the images anyway, and they're pngs to start with, why not just copy them? [NSFileManager copyItemAtPath:toPath:error:]Michaelamichaele
Great Idea..! I will check that nowWoodbridge
No it did not work. Assets URL are different from normal one.Woodbridge
You can copy the raw data using ALAssetRepresentation. The downside is photos are typically JPEG, not PNG.Hildegardehildesheim
W
1

I mailed this issue to Apple and they asked me to introduce sleep cycles between every allocation. Add sleep before allocation.

Woodbridge answered 2/2, 2012 at 11:32 Comment(1)
I also tried this but not helpful..Please provide other way to solve this...Thanks in advanceSodium

© 2022 - 2024 — McMap. All rights reserved.