从照片库的特定相册加载100张以上图像时出现内存警告

时间:2013-08-27 07:17:07

标签: iphone uiimage alassetslibrary assets

请找到以下代码从照片库中提取图像

- (void) initilizeAssetForPhotoLibrary {

if (!assets) {
    assets = [[NSMutableArray alloc] init];
} else {
    [assets removeAllObjects];
}

ALAssetsGroupEnumerationResultsBlock assetsEnumerationBlock = ^(ALAsset *result, NSUInteger index, BOOL *stop) {

    if (result) {
        [assets addObject:result];
    }
};

ALAssetsFilter *onlyPhotosFilter = [ALAssetsFilter allPhotos];
[assetsGroup setAssetsFilter:onlyPhotosFilter];
[assetsGroup enumerateAssetsUsingBlock:assetsEnumerationBlock];

}

- (NSMutableArray *) getImagesFromPhotoLibrary {


for(int i=0; i<assets.count; i++) {

ALAsset *asset = [assets objectAtIndex:i];

ALAssetRepresentation *assetRepresentation = [asset defaultRepresentation];    
UIImage *getImage = [UIImage imageWithCGImage:[assetRepresentation fullScreenImage] scale:[assetRepresentation scale] orientation:(UIImageOrientation)[assetRepresentation orientation]];
[arrImages addObject:getImage];
}

return arrImages;

}

实际上我的要求是我需要在应用程序中加载特定相册中的所有图像以创建幻灯片。

当我加载少于100张图像时,它可以正常工作但在上面它会收到内存警告,然后崩溃。

如果有人有不同想法加载图片和减少内存消耗,请帮助我。谢谢你提前。

2 个答案:

答案 0 :(得分:2)

保留内存中的所有图像都不行,内存不足。

您需要使用ALAssetRepresentation图像填充数组,并仅在准备好显示图像时加载图像。这样,您将只在内存中显示您正在显示的图像。

答案 1 :(得分:-1)

然后记下下面的代码并让我知道它是否正常工作!!!

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
                                         (unsigned long)NULL), ^(void) {

     for(int i=0; i<assets.count; i++) {

    ALAsset *asset = [assets objectAtIndex:i];

    ALAssetRepresentation *assetRepresentation = [asset defaultRepresentation];    
    UIImage *getImage = [UIImage imageWithCGImage:[assetRepresentation fullScreenImage] scale:[assetRepresentation scale] orientation:(UIImageOrientation)[assetRepresentation orientation]];
   [arrImages addObject:getImage];
 }
  return arrImages;

});

快乐编码...... !!!!