应用程序每次在IOS中都会因内存压力而崩溃?

时间:2014-02-04 11:44:51

标签: ios crash memory-pressure

我使用以下功能从视图生成图像

-(void)preparePhotos 
{
    [assetGroup enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop)
     {
         if(result == nil)
         {
             return;
         }

         NSMutableDictionary *workingDictionary = [[NSMutableDictionary alloc] init];
         UIImage *img = [[UIImage imageWithCGImage:[[result defaultRepresentation] fullResolutionImage]] resizedImageToSize:CGSizeMake(600, 600)];
         [workingDictionary setObject:img forKey:@"UIImagePickerControllerOriginalImage"];
         [appdelegate.arrImageData addObject:workingDictionary];
     }]; 
}

但随着调用此函数的次数增加,app崩溃了。如何优化此功能或任何替代功能以从设备库中获取图像,这不会导致像这样的crash.function调用。

[self performSelectorInBackground:@selector(preparePhotos) withObject:nil];

1 个答案:

答案 0 :(得分:1)

如果您想要从照片库中获取所有图像,那么您只应存储其资产网址而不是图像本身。假设您将Assets url存储在名为photoAssets的数组中,而不是通过仅传递索引来调用此方法:

- (UIImage *)photoAtIndex:(NSUInteger)index
{
    ALAsset *photoAsset = self.photoAssets[index];

    ALAssetRepresentation *assetRepresentation = [photoAsset defaultRepresentation];

    UIImage *fullScreenImage = [UIImage imageWithCGImage:[assetRepresentation fullScreenImage]
                                                   scale:[assetRepresentation scale]
                                             orientation:UIImageOrientationUp];
    return fullScreenImage;
}

有关更多信息或参考,请参阅PhotoScrollerMyImagePicker