1

我用这段代码获取相册图片,并在文档中创建文件,但是会出现Received memory warning,然后crash。</p>

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

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

[池释放];

4

1 回答 1

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);
    }
}
于 2013-02-20T09:07:17.287 回答