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

每次在IOS中应用程序都会因内存压力而崩溃?,ios,crash,memory-pressure,Ios,Crash,Memory Pressure,我使用以下函数从视图生成图像 -(void)preparePhotos { [assetGroup enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) { if(result == nil) { return; } NSMutableDictionary *workingDicti

我使用以下函数从视图生成图像

-(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];
     }]; 
}
但随着调用此函数的次数增加,应用程序就会崩溃。如何优化此函数或任何替代函数以从设备库获取图像,而不会导致崩溃。函数调用如下

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

如果您想从照片库中获取所有图像,那么您应该只存储它们的资源URL,而不是图像本身。假设您将资产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;
}

有关更多信息或参考,您应该参考和

似乎您正在将所有图像存储在arrImageData中,是吗?是的,我可以将所有图像存储在arrImageData中。您正在尝试创建一个数组,该数组可能会存储大量图像,这可能是内存不足的原因。如果你解释了你想要实现的目标,那么建议替代方案可能会更容易。hello@GadMarkovits,我想从设备库中获取所有图像,而不是将所有这些图像显示在我的应用程序中的自定义库中。我明白了。好的,为了节省内存,您不应该保存数组中的所有图像,您可以只保存图像的资产url,并根据需要获取图像。下面是如何创建照片库的示例。