Dispatch_async 和 UI

发布于 2024-12-01 13:56:37 字数 929 浏览 0 评论 0原文

我发出警报,通知用户有关保存操作的信息,我将其添加到视图中,保存一些图像并消除警报。然而它并没有按照我希望的方式工作。首先在控制台中查看 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生死何惧 2024-12-08 13:56:37

您应该简单地使用 dispatch_sync 而不是 dispatch_async。直到该块在主线程上执行完毕后,该函数才会返回。

You should simply use dispatch_sync instead of dispatch_async. That function will not return until the block has executed on the main thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文