ios编程:使用线程将多个图像添加到库中

在Xcode中,当我尝试向我的库中添加5个以上的图像时 ,它会给我以下错误:

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 *函数:

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

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

我尝试过使用NSThreads并做了:

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

但它仍然无效。

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

谢谢。

编辑:

尝试了dispatch_async。 这是错的吗?

 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 ,并实现“异步循环”,它按顺序调用许多异步方法。

 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]; }]; } 

用法:

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

我建议使用NSOperationQueue并使用NSOperationQueue的值。 这样,您可以控制对库的同时写入次数,而不是压倒它。

如果你使用线程,甚至是GCD,你需要自己实现这个逻辑。 更多代码 – >更多引入错误的机会。

Dispatch_async是后台可以做的事情,所以我把你的for{}和函数调用放在dispatch_async体内,所以在dispatch_async放入dispatch async会将你的图像存储在相册中,就像刚刚运行一样。 但是在单独的线程上。

  dispatch_queue_t myQueue = dispatch_queue_create("com.cropr.myqueue", 0); dispatch_async(myQueue, ^{ for (UIImage * image in images) UIImageWriteToSavedPhotosAlbum((__bridge UIImage *)(image),self,nil,nil); }); } 

你也可以试试这个。 我认为会更好,看看它是否有效,然后添加你想要处理的错误。

另外我认为这是你想要的答案: iOS:dispatch_async和UIImageWriteToSavedPhotosAlbum