Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
437 views
in Technique[技术] by (71.8m points)

uiimage - Memory issue in using UIImagePNGRepresentation

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...