Dispatch_async 和 UI
我发出警报,通知用户有关保存操作的信息,我将其添加到视图中,保存一些图像并消除警报。然而它并没有按照我希望的方式工作。首先在控制台中查看 ofc 下面的代码,我得到“已保存..”,然后是“dispath”。我想获得相反的效果,首先获得“dispath”,然后“保存..”(因此在屏幕上写警报,然后保存在后台,最后关闭警报)。但我更改了 imageView1 的图像,所以我无法将合并移出 dispath_async,因为它是一个 UI 操作..那么该怎么做..?我需要首先合并图像,然后保存它们以及所有这些计算时间以保持警惕。
//adding alert to view
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{
dispatch_async(dispatch_get_main_queue(), ^{
//i want this to complete->
imageView1.image = [self merge:imageView1.image and:imageView2.image];
NSLog(@"dispatch");
});
//and then to do this action->
UIImageWriteToSavedPhotosAlbum(imageView1.image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
NSLog(@"saved..");
dispatch_async(dispatch_get_main_queue(), ^{
[alert dismissWithClickedButtonIndex:0 animated:YES];
});
});
I made alert informing user about saving action, i add it to view, save some image and dismiss alert. However it's not working in the way i hoped it would. Looking at code below ofc firstly in console i get "saved.." then "dispath". I would like to get opposite effect firstly get "dispath" then "saved.." (so write alert on screen, then save in background and finally dismiss alert). But i change image of imageView1, so i cant move merging out of dispath_async because its an UI action.. how to do it then..? I need to firstly merge images and after it to save them and all of this calculating time to keep alert in front.
//adding alert to view
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{
dispatch_async(dispatch_get_main_queue(), ^{
//i want this to complete->
imageView1.image = [self merge:imageView1.image and:imageView2.image];
NSLog(@"dispatch");
});
//and then to do this action->
UIImageWriteToSavedPhotosAlbum(imageView1.image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
NSLog(@"saved..");
dispatch_async(dispatch_get_main_queue(), ^{
[alert dismissWithClickedButtonIndex:0 animated:YES];
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该简单地使用
dispatch_sync
而不是dispatch_async
。直到该块在主线程上执行完毕后,该函数才会返回。You should simply use
dispatch_sync
instead ofdispatch_async
. That function will not return until the block has executed on the main thread.