检查ALAssetsLibrary是否为空

时间:2013-02-08 07:45:16

标签: iphone xcode

我正在使用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");
    }];

最后一个++变量之后的行。我正在使用newscreenshot图像检查lastimage以禁止用户在截屏后使用应用程序

1 个答案:

答案 0 :(得分:0)

这是给你错误的一行:

[NSIndexSet indexSetWithIndex:[group numberOfAssets]-1]

如果资产为零,则将索引设置为-1。您可以将该枚举块包装在条件中以测试numberOfAssets&gt;在继续之前0。此外,您还可以在此处查看其他有用的问题。例:How to get the latest photo in iPhone Library?