我正在使用ALAssetsLibrary,当它是empty.How的时候,app崩溃了,我要检查它是否为空吗?
-(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与新截图图像,以禁止用户在截图后使用应用程序
发布于 2013-02-08 18:29:57
这是给出错误的那一行:
[NSIndexSet indexSetWithIndex:[group numberOfAssets]-1]如果资产为零,则将索引设置为-1。在继续之前,您可以将该枚举块包装在一个条件中,以测试numberOfAssets是否>0。此外,您还可以在这里找到其他有用的问题。示例:How to get the latest photo in iPhone Library?
https://stackoverflow.com/questions/14767891
复制相似问题