ios编程:使用线程将多个图像添加到库中 [英] ios programming: Using threads to add multiple images to library

查看:298
本文介绍了ios编程:使用线程将多个图像添加到库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Xcode中,当我尝试将超过5张图片添加到我的库时,它会出现以下错误:

In Xcode, when I'm trying to add more than 5 images to my library, it gives me the following error:

Error Domain=ALAssetsLibraryErrorDomain Code=-3301 "Write busy" UserInfo=0xa706aa0 {NSLocalizedRecoverySuggestion=Try to write again, NSLocalizedFailureReason=There was a problem writing this asset because the writing resources are busy., NSLocalizedDescription=Write busy, NSUnderlyingError=0xa770110 "Write busy"}

为了解决这个问题,我想线程可以解决我的问题。文档说明我可以使用POSIX线程或 NSThreads 。当我尝试使用POSIX线程时,我将我的线程设置为可连接,并且我正在创建一个void *函数:

In order to solve this problem, I figured out threads would solve my problems. The documentation states that I can use POSIX threads or NSThreads. When I try using POSIX threads, I set my threads to be joinable, and I'm creating a void * function:

void * myFunc (void * image)
{
       UIImageWriteToSavedPhotosAlbum((__bridge UIImage *)(image),self,nil,nil);
       pthread_exit(NULL);
       return NULL;
}

我也在等待线程结束。但仍然只写了5张图片。

I am also waiting for the thread to end. But still only 5 images are written.

我尝试过使用 NSThreads 并做了:

[self performSelectorOnMainThread:@selector(myFunc:) withObject:image waitUntilDone:YES];

但它仍然不起作用。

我的问题有答案吗?这对我的工作至关重要。

Is there an answer to my problem? It's crucial to my work.

谢谢。

编辑:

也尝试了dispatch_async。这是错的吗?

Tried dispatch_async too. Is it wrong?

dispatch_queue_t myQueue = dispatch_queue_create("com.cropr.myqueue", 0);

for (UIImage * image in images) {

        dispatch_async(myQueue, ^{

            [self.library saveImage:image toAlbum:@"Cropr" withCompletionBlock:^(NSError *error) {
                if (error!=nil) {
                    NSLog(@"Big error: %@", [error description]);
                }
            }];

        });

    }

我需要添加什么?

推荐答案

您可以尝试随后编写所有图像,而不是同时。以下代码使用 ALAssetsLibrary ,并实现了一个异步循环,它在 sequence 中调用了许多异步方法。

You may try to write all your images subsequently, instead of simultaneously. The following code utilizes ALAssetsLibrary, and implements an "asynchronous loop" which invokes a number of asynchronous methods in sequence.

typedef void (^completion_t)(id result);

- (void) writeImages:(NSMutableArray*)images 
          completion:(completion_t)completionHandler {
    if ([images count] == 0) {
        if (completionHandler) {
            // Signal completion to the call-site. Use an appropriate result,
            // instead of @"finished" possibly pass an array of URLs and NSErrors 
            // generated below  in "handle URL or error".
            completionHandler(@"finished");  
        }
        return;
    }

    UIImage* image = [images firstObject];
    [images removeObjectAtIndex:0];

    [self.assetsLibrary writeImageToSavedPhotosAlbum:image.CGImage 
                                         orientation:ALAssetOrientationUp
                                     completionBlock:^(NSURL *assetURL, NSError *error)
    {
        // Caution: check the execution context - it may be any thread,
        // possibly use dispatch_async to dispatch to the main thread or
        // any other queue.

        // handle URL or error
        ...
        // next image:
        [self writeImages:images completion:completionHandler];
    }];
}



用法:



Usage:

[foo writeImages:[foo.images mutableCopy] completion:^(id result){
    // Caution: check the execution context - it may be any thread
    NSLog(@"Result: %@", result);   
}];

这篇关于ios编程:使用线程将多个图像添加到库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆