简体   繁体   English

Dispatch_async和UI

[英]Dispatch_async and UI

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". 首先在控制台中查看ofc下面的代码,我“保存..”,然后“混乱”。 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). 我想得到相反的效果,首先得到“ dispath”,然后得到“ saved ..”(因此在屏幕上写警报,然后保存在后台,最后关闭警报)。 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..? 但是我更改了imageView1的图像,所以我无法将合并从dispath_async中移出,因为它是一个UI动作..然后怎么做..? 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];
    });
});

You should simply use dispatch_sync instead of dispatch_async . 您应该只使用dispatch_sync而不是dispatch_async That function will not return until the block has executed on the main thread. 直到该块在主线程上执行后,该函数才会返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM