ALAssetsLibrary 的问题
Posted
技术标签:
【中文标题】ALAssetsLibrary 的问题【英文标题】:Issue with ALAssetsLibrary 【发布时间】:2014-07-05 07:40:02 【问题描述】:我正在尝试访问照片库中拍摄的最后一张照片,一切正常,除非照片库没有照片,我的应用程序崩溃了!如何找到停止崩溃的解决方案?这是我的代码:
-(void)importLastImage
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
// Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop)
// Within the group enumeration block, filter to enumerate just photos.
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
// Chooses the photo at the last index
[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:([group numberOfAssets]-1)]
options:0
usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop)
// The end of the enumeration is signaled by asset == nil.
if (alAsset)
ALAssetRepresentation *representation = [alAsset defaultRepresentation];
latestPhoto = [UIImage imageWithCGImage:[representation fullResolutionImage]];
else
];
failureBlock: ^(NSError *error)
// Typically you should handle an error more gracefully than this.
NSLog(@"No groups");
];
【问题讨论】:
【参考方案1】:您是否尝试将您的方法封装到if condition
中?
-(void)importLastImage
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
// Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop)
// Within the group enumeration block, filter to enumerate just photos.
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
// Chooses the photo at the last index
if ([group numberOfAssets] > 0)
[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:([group numberOfAssets]-1)] options:0 usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop)
// The end of the enumeration is signaled by asset == nil.
if (alAsset)
ALAssetRepresentation *representation = [alAsset defaultRepresentation];
latestPhoto = [UIImage imageWithCGImage:[representation fullResolutionImage]];
else
];
failureBlock: ^(NSError *error)
// Typically you should handle an error more gracefully than this.
NSLog(@"No groups");
];
【讨论】:
以上是关于ALAssetsLibrary 的问题的主要内容,如果未能解决你的问题,请参考以下文章
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; - 错误
iOS - ALAssetsLibrary 使用的缓存数据未更新