Received memory warning in image file

I use this code to get albums and create a file in documents, but a warning with memory will be received, and then crash.

Here is the code I used. Can someone tell me what I did wrong?

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror){ NSLog(@"error occour =%@", [myerror localizedDescription]); }; ALAssetsGroupEnumerationResultsBlock groupEnumerAtion = ^(ALAsset *result, NSUInteger index, BOOL *stop){ if (result!=NULL) { //we can get all the things in the defaultRepresentation such as size info in UTI } //just fetching photos if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) { //copy image to the path:Documents/DMS/Photo ALAssetRepresentation *rep = [result defaultRepresentation]; NSString *tt = [rep filename]; NSString *fullPath = [pathPhoto stringByAppendingFormat:@"/%@",tt]; if (![[NSFileManager defaultManager] fileExistsAtPath:fullPath]){ UIImage *image = [[UIImage alloc]initWithCGImage:[rep fullScreenImage]]; NSData *imageData = UIImagePNGRepresentation(image); [image release]; [[NSFileManager defaultManager] createFileAtPath:fullPath contents:imageData attributes:nil]; NSLog(@"Creat image file fullPath================%@",fullPath); //imageData = nil; [imageData release]; }else{ NSLog(@"---------------------the image is Exist"); } } }; ALAssetsLibraryGroupsEnumerationResultsBlock libraryGroupsEnumeration = ^(ALAssetsGroup* group, BOOL* stop){ if (group == nil) { return; } if (group!=nil) { [group enumerateAssetsUsingBlock:groupEnumerAtion]; } NSLog(@"finish--------------------------------------------"); return; }; ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init]; [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:libraryGroupsEnumeration failureBlock:failureblock]; [library release]; 

[pool release];

+1
source share
1 answer

Since you are saying that you are using the code that you placed inside the loop, I suppose that it happens that your application gets killed because too many automatically allocated objects are allocated inside the loop.

You can try using the auto resource pool:

 for (...) { @autoreleasepool { <your code here> } } 

so that the autoresist pool is cleared at each iteration (instead of growing throughout the cycle).

EDIT:

 if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) { ALAssetRepresentation *rep = [result defaultRepresentation]; CGImageRef iref = [rep fullScreenImage]; NSString *tt = [rep filename]; if (iref) { UIImage *image = [UIImage imageWithCGImage:iref]; if(!image) { NSLog(@"---------------------the imageData is nil"); } else { NSData *imageData = UIImagePNGRepresentation(image); NSString *fullPath = [pathPhoto stringByAppendingFormat:@"/%@.png",tt]; NSLog(@"fullPath================%@",fullPath); if (![[NSFileManager defaultManager] fileExistsAtPath:fullPath]) { [[NSFileManager defaultManager] createFileAtPath:fullPath contents:imageData attributes:nil]; NSLog(@"Creat image file fullPath================%@",fullPath); } } CGImageRelease(iref); } } 
0
source

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


All Articles