我使用這個代碼,以獲得從cameraRoll最新照片:應用與「NSRangeException」崩潰時庫是空的
- (void) getTheLatestPhoto{
ALAssetsLibrary *cameraRoll = [[ALAssetsLibrary alloc] init];
[cameraRoll enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *images, BOOL *stop) {
[images setAssetsFilter:[ALAssetsFilter allPhotos]];
[images enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:[images numberOfAssets] - 1] options:0 usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop)
{
if (result) {
ALAssetRepresentation *rep = [result defaultRepresentation];
self.sharingImage = [UIImage imageWithCGImage:[rep fullScreenImage]];
}
}];
}
failureBlock: ^(NSError *error) {
NSLog(@"An error occured: %@",error);
}];
}
正因爲如此,如果庫cameraRoll是空的,應用程序將崩潰這個錯誤:
Terminating app due to uncaught exception 'NSRangeException', reason: 'indexSet count or lastIndex must not exceed -numberOfAssets'
我明白我的索引集比numberOfAssets走高,因此:
[images enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:[images numberOfAssets] - 1]
在這一點上,我的問題是「我在哪裏提出了一個條件,允許我檢查資產是否爲零?」
如果cameraRoll有超過零,我會讓碼繼續,但如果計數爲零,我想處理這個樣子的反對讓它崩潰:
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"Sorry, there are no photo in Camera Roll" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
感謝你的幫助!
只是,崩潰前行。 – Wain 2013-05-10 13:03:11