ALAssetsLibrary を使用していますが、空のときにアプリがクラッシュします。空かどうかを確認するにはどうすればよいですか?
-(void)getLastImageName1
{
// if (val < 10) {
// NSLog(@"getLastImageName1\n");
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
// Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
last =1;
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
// Within the group enumeration block, filter to enumerate just photos.
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
if (last == 1) {
// NSLog(@"last\n");
last++;
// 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) {
NSString *lastImgNew = alAsset.defaultRepresentation.filename;
// NSLog(@"current img name %@\n",lastImgNew);
NSString *plistPath1 = [DOCUMENT_DIR_PATH stringByAppendingPathComponent:@"previouslastimagename.plist"];
NSArray *lastImg = [NSArray arrayWithContentsOfFile:plistPath1];
// NSLog(@"get pre lastimg %@\n",lastImg);
// NSArray *lastImg = [[DBModel database]getPreviousName];
// NSLog(@"get lastImg %@\n",lastImg);
if ([lastImg count] != 0) {
// NSLog(@"count\n");
if ([[lastImg objectAtIndex:0] isEqualToString:lastImgNew]) {
// NSLog(@"img eql\n");
// UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"equal" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
// [alert show];
[self hideImage];
// }
}
else
{
// UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:[NSString stringWithFormat:@"pre %@ current %@",[lastImg objectAtIndex:0],lastImgNew] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
// [alert show];
// NSLog(@"img not eql\n");
[self performSelectorOnMainThread:@selector(displayAlert) withObject:nil waitUntilDone:YES];
}
}
}
}];
}
} failureBlock: ^(NSError *error) {
// Typically you should handle an error more gracefully than this.
// NSLog(@"No groups");
}];
last++ 変数の後の行。ユーザーがスクリーンショットを撮った後にアプリを使用できないように、新しいスクリーンショット画像で lastimage をチェックしています