在创建映像文件中收到内​​存警告

时间:2013-02-20 08:09:56

标签: ios

我使用此代码获取相册图片,并在文档中创建文件,但会有Received内存警告,然后崩溃。

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

 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 个答案:

答案 0 :(得分:0)

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

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

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