且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

iOS UIActivityViewController 使用 UIActivityItemProvider “忘记"项目

更新时间:2023-12-02 19:37:58

这个问题的原因很简单.

The reason for this issue is pretty simple.

应用程序可用的写入器线程数量有限,因此同时写入过多图像可能会由于写入忙"错误而失败.您可以通过尝试使用 UIImageWriteToSavedPhotosAlbum(或相应的 Asset/Photos 框架对应项)手动编写这些图像来验证这一点.因此,调整图像大小只是隐藏了真正的问题,因为编写较小的图像需要更少的时间.

There are limited number of writer threads available to use by the app, so writing too many images simulteneously may fail due to "Write Busy" error. You can verify that by trying to manually write these images using UIImageWriteToSavedPhotosAlbum (or corresponding Asset/Photos framework counterparts). So, resizing images just hides the real problem because writing a smaller image takes less time.

该问题可以通过限制写入者的数量和/或在出现错误时重试来解决.另请注意, -[UIActivityItemProvider item] 是一个阻塞调用,但没有同步方式可以开箱即用地写入库.这可以通过 dispatch_semaphore_wait 、 NSCondition 等来处理.或者,如果您使用的是 ReactiveCocoa,只需调用 waitUntilCompleted.

The problem can be solved by liming the number of writers and/or retrying in case of error. Also note that -[UIActivityItemProvider item] is a blocking call, but there are no synchronous ways to write to the gallery out of the box. This can be handled with dispatch_semaphore_wait , NSCondition, etc. Or, in case you're using ReactiveCocoa, by simply calling waitUntilCompleted.