【发布时间】:2025-12-23 08:55:10
【问题描述】:
在 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]);
}
}];
});
}
我需要添加什么?
【问题讨论】:
-
你试过异步调度吗?
-
这里是 dispatchAsync jeffreysambells.com/2013/03/01/…的指南
-
是的。我已经做了。使用了上面的指南,这很有意义,但仍然没有雪茄......
-
甚至尝试在 dispatchAsync 中放置一个 while 条件,告诉它一遍又一遍地尝试写入,从而导致无限循环!我知道为什么??
-
好的,只需用代码更新您的问题(当您使用 GCD 时),我会给您一个答案(使其正常工作)。
标签: ios multithreading image