首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检查ALAssetsLibrary是否为空

检查ALAssetsLibrary是否为空
EN

Stack Overflow用户
提问于 2013-02-08 15:45:16
回答 1查看 322关注 0票数 0

我正在使用ALAssetsLibrary,当它是empty.How的时候,app崩溃了,我要检查它是否为空吗?

代码语言:javascript
复制
   -(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与新截图图像,以禁止用户在截图后使用应用程序

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-08 18:29:57

这是给出错误的那一行:

代码语言:javascript
复制
[NSIndexSet indexSetWithIndex:[group numberOfAssets]-1]

如果资产为零,则将索引设置为-1。在继续之前,您可以将该枚举块包装在一个条件中,以测试numberOfAssets是否>0。此外,您还可以在这里找到其他有用的问题。示例:How to get the latest photo in iPhone Library?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14767891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档