OStack程序员社区-中国程序员成长平台

标题: iphone - ALAssetsLibrary 太慢 - Objective-C [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 23:18
标题: iphone - ALAssetsLibrary 太慢 - Objective-C

从相机胶卷中加载 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);
}];



Best Answer-推荐答案


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

所以在 ALAssetsLibrary block 中使用 -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 performSelectorOnMainThreadselector(usePhotolibraryimage withObject:image waitUntilDone:NO];
        }];
    }

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

- (void)usePhotolibraryimageUiImage *)myImage{

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

注意:看this issue too .

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






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4