在创build图像文件中收到内​​存警告

我使用这个代码获取相册图片,并在文档中创build文件,但会收到内存警告,然后崩溃。

这是我使用的代码。 谁能告诉我我做错了什么?

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]; 

[游泳池释放];

既然你说你正在使用你在循环中发布的代码,我想这会发生什么是你的应用程序正在被杀死,因为太多的自动释放的对象被分配在循环内部。

你可以尝试使用自动释放池:

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

这样自动释放池在每次迭代时都被清除(而不是在整个循环执行期间一直增长)。

编辑:

 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); } }