IOS / Objective-C:将UIImage从后台传递到主队列

时间:2017-10-24 17:24:22

标签: ios multithreading variables

我走到网上抓取后台队列中的图像。如果找到图像,我想在主队列中使用它。但是,我发现主队列中的对象在后台队列中无法识别。我想我需要使用块变量,但我发现这些非常令人困惑。任何人都可以建议正确的方法将从Web下载的图像传递回主队列吗?提前感谢任何建议。

UIImage*myImage; //image object I want to use on main queue.
  dispatch_async(kBgQueue, ^{ //open 4
                NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:picURL]];
                if (imgData) { //open 5  Is download complete?
                    UIImage *imageFromWeb = [UIImage imageWithData:imgData];
                    if (imageFromWeb) { //open 6
                        NSLog(@"we got an image from contact pic url%@ and will now save it",picURL);
                        [self saveImage:imageFromWeb asPicName:picname];
                        dispatch_async(dispatch_get_main_queue(), ^{ //open 7
                            myImage = imageFromWeb; //THROWS ERROR HERE 'variable is not assignable...'

                        }); //close 7
                    } //close 6
                } //close 5
            }); //close 4

0 个答案:

没有答案