本文介绍了ALAssetsLibrary太慢了 - Objective-C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从相机胶卷加载10-20全屏图像,保存照片有什么快捷方式?

What is a fast way to load 10-20 fullscreen images from a camera roll, saved photos?

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

I'm using this code, but to load 10 photos I need to wait about 5-10 seconds. I'm using 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 库将在单独的线程上运行。因此,与相关及其他内容进行沟通可能需要一段时间。

The ALAssetsLibrary library will run on a separate thread. So it may take time to communicate with the UI related and other stuff.

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

So use -performSelectorOnMainThread:withObject:waitUntilDone: inside the ALAssetsLibrary block.

更改以下代码

Change your code as below

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
}

注意:查看。

这篇关于ALAssetsLibrary太慢了 - Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 20:00