iOS版:dispatch_async和UIImageWriteToSavedPhotosAlbum [英] iOS: dispatch_async and UIImageWriteToSavedPhotosAlbum

查看:620
本文介绍了iOS版:dispatch_async和UIImageWriteToSavedPhotosAlbum的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚学如何分配任务的线程中,或异步分派。我明白了触及的任何操作视图,必须在主线程中完成的。什么: UIImageWriteToSavedPhotosAlbum 我会假设这可能​​会在后台线程来完成,但是是我错了?

此外,如果它应该在后台线程完成,是否有这两个调用之间的差以下时,为一个保存一个UIImage和其他从视图可以节省一个UIImage?

  UIImageWriteToSavedPhotosAlbum(_someUIImage,无,无,无);UIImageWriteToSavedPhotosAlbum(_imageView.image,无,无,无);

这是我使用此设置在主线程,并在后台任务运行的HUD办法,那就是我的意图。

  [HUD_ code showMessage:@保存图像];
dispatch_queue_t concurrentQueue =
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_async(concurrentQueue,^ {
        UIImageWriteToSavedPhotosAlbum(someUIImage,无,无,无);
            dispatch_async(dispatch_get_main_queue(),^ {
                [HUD_ code解雇];
         });
});


解决方案

<一个href=\"http://developer.apple.com/library/ios/#documentation/uikit/reference/UIKit_Framework/Introduction/Introduction.html#//apple_ref/doc/uid/TP40006955-CH1-SW2\"相对=nofollow> UIKit类都记录是从主线程可用的只有,除非另有说明。 (例如,<一个href=\"http://developer.apple.com/library/ios/#documentation/uikit/reference/UIFont_Class/Reference/Reference.html\"相对=nofollow> UIFont 被证明是线程安全的。)

有关于UIKit中的功能的线程安全性没有显式声明毯的(来自不同的班级),所以它不是安全的假设,他们一般是线程安全的。事实上,一些UIKit的功能,如<一个href=\"http://developer.apple.com/library/ios/#documentation/uikit/reference/UIKitFunctionReference/Reference/reference.html#//apple_ref/doc/uid/TP40006894\"相对=nofollow> UIGraphicsBeginImageContext ,明确记载是线程安全的,暗示的UIKit功能的不可以一般线程安全的。

由于 UIImageWriteToSavedPhotosAlbum 可发送异步完成的消息,你应该称呼它在主线程上,并使用其完成支持来执行 [HUD_ code驳回​​]

Just learning how to allocate tasks among threads, or dispatch asynchronously. I understand that any operation that "touches" a view must be done on the main thread. What about: UIImageWriteToSavedPhotosAlbum? I would assume this could be done on a background thread, but am I mistaken?

Also, if it should be done on a background thread, is there a difference between these two calls below, as one saves a UIImage and the other saves a UIImage from a view?

UIImageWriteToSavedPhotosAlbum(_someUIImage ,nil,nil,nil);

UIImageWriteToSavedPhotosAlbum(_imageView.image ,nil,nil,nil);

By the way I am using this setup to run an HUD in the main thread and to tasks in the background, that is my intention.

[HUD_code showMessage:@"saving image"];
dispatch_queue_t concurrentQueue =
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(concurrentQueue, ^{
        UIImageWriteToSavedPhotosAlbum(someUIImage ,nil,nil,nil);
            dispatch_async(dispatch_get_main_queue(), ^{
                [HUD_code dismiss];
         });
});

解决方案

UIKit classes are documented to be usable from the main thread only, except where documented otherwise. (For example, UIFont is documented to be thread-safe.)

There's no explicit blanket statement about the thread safety of UIKit functions (as distinct from classes), so it's not safe to assume they generally thread-safe. The fact that some UIKit functions, like UIGraphicsBeginImageContext, are explicitly documented to be thread-safe, implies that UIKit functions are not generally thread-safe.

Since UIImageWriteToSavedPhotosAlbum can send an asynchronous completion message, you should just call it on the main thread and use its completion support to perform your [HUD_code dismiss].

这篇关于iOS版:dispatch_async和UIImageWriteToSavedPhotosAlbum的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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