2

从相机胶卷中加载 10-20 张全屏图像、保存照片的快速方法是什么?

我正在使用此代码,但要加载 10 张照片,我需要等待大约 5-10 秒。我用的是 iPhone 4S。

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    if(_savedPhotos.count>=11) *stop = YES;
    [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *needToStop) {
        NSLog(@"%d",index);
        if(_savedPhotos.count<11)
        {
            UIImage *image = [UIImage imageWithCGImage:result.defaultRepresentation.fullScreenImage];
            [_savedPhotos addObject:image];
        }
        else
        {
            *needToStop = YES;
        }
    }];
} failureBlock:^(NSError *error) {
    NSLog(@"%@",error.description);
}];
4

1 回答 1

4

ALAssetsLibrary库将在单独的线程上运行。因此,与UI相关和其他内容进行通信可能需要时间。

所以-performSelectorOnMainThread:withObject:waitUntilDone:在 ALAssetsLibrary 块内使用。

更改您的代码如下

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *needToStop) {
            NSLog(@"%d",index);
            UIImage *image = [UIImage imageWithCGImage:result.defaultRepresentation.fullScreenImage];
            [self performSelectorOnMainThread:@selector(usePhotolibraryimage:) withObject:image waitUntilDone:NO];
        }];
    }

    failureBlock:^(NSError *error) {
           NSLog(@"%@",error.description);
    }];

- (void)usePhotolibraryimage:(UiImage *)myImage{

    //Do your all UI related and all stuff here
}

注意:也看这个问题

于 2012-10-22T09:43:57.923 回答