Я пытаюсь загрузить и показать последнюю фотографию из фотобиблиотеки (Camera roll) в UIImageView
, все работает отлично! кроме одного! если в библиотеке нет изображения, тогда приложение падает! Вот мой код:Detect Photo Library is empty
-(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]];
_lastImage.image = latestPhoto;
}else {
//no image found !
}
}];
}
failureBlock: ^(NSError *error) {
// Typically you should handle an error more gracefully than this.
NSLog(@"No groups");
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"ERROR" message:@"No Image found" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
[alert show];
}];
}
Попробуйте добавить некоторые условия вдоль линии 'если ([группа numberOfAssets])' перед вызовом 'enumerateAssetsAtIndexes: варианты: usingBlock:' и посмотреть, если он решает ваши вопрос. – AMI289