从相机胶卷,保存的照片加载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);
}];

最佳答案

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

因此,请在ALAssetsLibrary块中使用 -performSelectorOnMainThread:withObject:waitUntilDone:

如下更改您的代码

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
}

注意:看this issue too

关于iphone - ALAssetsLibrary太慢-Objective-C,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13008251/

10-10 06:22