Import multiple source resolution images: low memory issue

I use ChuteSDK to import multiple images from a photo library something like this:

-(void)doneSelected{ NSMutableArray *returnArray = [NSMutableArray array]; [self showHUD]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void){ for(id object in [self selectedAssets]){ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; if([object isKindOfClass:[GCAsset class]]){ ALAsset *asset = [object alAsset]; NSMutableDictionary* temp = [NSMutableDictionary dictionary]; [temp setObject:[[asset defaultRepresentation] UTI] forKey:UIImagePickerControllerMediaType]; [temp setObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage] scale:1 orientation:(UIImageOrientation)[[asset defaultRepresentation] orientation]] forKey:UIImagePickerControllerOriginalImage]; [temp setObject:[[asset defaultRepresentation] url] forKey:UIImagePickerControllerReferenceURL]; [returnArray addObject:temp]; } [pool release]; } dispatch_async(dispatch_get_main_queue(), ^(void) { if(delegate && [delegate respondsToSelector:@selector(PhotoPickerPlusController:didFinishPickingArrayOfMediaWithInfo:)]) [delegate PhotoPickerPlusController:[self P3] didFinishPickingArrayOfMediaWithInfo:returnArray]; [self hideHUD]; }); }); } 

But fullScreenImage gives me a smaller version of the original image, and if I use fullResolutionImage , this causes a low memory warning problem that causes the application to crash.

How can I get an image with original resolution without memory problems.

PS: I do not use ARC in my project.

+4
source share
2 answers

The 'returnArray' you specified is outside the autocomplete pool block. Now you add your image to the "temp" dictionary, which is inside the auto-release pool, but in the end you add this "temp" to the "returnArray", and therefore it increases the number of holds that actually causes a leak.

Even if you are working with images, keep one more thing in mind. When you use an image, it does not accept in memory what it shows as the file size, as many would expect (i.e. Something less than 3 MB for 2048 x 1536). Instead, it actually loads in raw format, taking memory based on the calculation as follows:

width x height xn bytes, where n is the number of bits taken to represent colors per pixel, mostly 4.

therefore, for the same 2048 x 1536 image, it will take 12 MB.

So, now check what the original resolution of the image you are talking about is, and calculate how many MB it will need, and change your code accordingly.

+2
source

You decompress all images at once. Wait until you need them.

0
source

Source: https://habr.com/ru/post/1494677/


All Articles