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

标签 ios crash memory-pressure

我正在使用以下函数从 View 生成图像

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

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

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

最佳答案

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

关于ios - IOS 中应用程序每次都会因内存压力而崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21551580/

相关文章:

ios - 在 SwiftUI 中构建 ListView 时出错

iphone - iOS中的路由跟踪模拟

iphone - 应用程序在模拟器上崩溃,但在 iPod 3.1.3 上运行良好。当应用程序在模拟器中运行时, super 类的 init 方法没有被命中(即使在调试时)

c - 为什么这段 C 代码会崩溃?

internet-explorer - IE11部分崩溃

linux - 即使在内存压力下,如何将可执行代码保留在内存中?在 Linux 中

ios - 由于使用不到 15MB 的内存压力而终止

javascript - 滚动 y 但固定到 x - ios 中的错误

ios - 无法从照片库中压缩 .mp4 视频