0

请找到以下用于从照片库中获取图像的代码

- (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 张时,它可以正常工作,但高于它会收到内存警告,然后它就崩溃了。

如果有人对加载图像和减少内存消耗有不同的想法,请帮助我。先谢谢了。

4

2 回答 2

2

将所有图像保留在内存中是不行的,只是没有足够的内存。

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

于 2013-08-27T07:29:53.753 回答
-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;

});

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

于 2013-08-27T07:25:28.973 回答