gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 23:10:29 25 4
gpt4 key购买 nike

我正在使用以下函数从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com