【问题标题】:Force execution of main queue in progressHandler (iOS)强制执行正在进行的主队列处理程序(iOS)
【发布时间】:2018-11-21 13:01:16
【问题描述】:

我正在使用 requestImageDataForAsset 从云端下载图像。我正在附加一个带有进度处理程序的 PHImageRequestOptions 以定期获得下载进度通知。在进度处理程序中,我正在绘制进度 UIImage 并使用此图像刷新 UIButton。

但是进度处理程序已加载到后台队列中,我需要在主线程上刷新 UIButton 图像。

我的问题是主线程只有在完整下载完成后才会执行。

有没有办法在给定时刻强制执行主线程?

        PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
        options.progressHandler = ^(double progress, NSError * _Nullable error, BOOL * _Nonnull stop, NSDictionary * _Nullable info) {
            NSLog(@"Progress: %f %%",progress*100.f);
            UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
            CGContextRef ctx = UIGraphicsGetCurrentContext();

            <create UIImage>
            progressImg=UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"Updating button image");
                [self->testButton setImage:progressImg forState:UIControlStateNormal];
                [self->testButton setNeedsDisplay];
            });
        };

        [[PHImageManager defaultManager] requestImageDataForAsset:asset options:options resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info)
        {
             <actions>
        }];

接收输出:

2018-06-12 10:30:34.765372+0300 App[12894:2931379] Progress: 0.000000 %
2018-06-12 10:30:34.828532+0300 App[12894:2931379] Progress: 0.000000 %
2018-06-12 10:30:35.724429+0300 App[12894:2931379] Progress: 0.000000 %
2018-06-12 10:30:41.703484+0300 App[12894:2931379] Progress: 0.042285 %
2018-06-12 10:30:44.241504+0300 App[12894:2931379] Progress: 5.973034 %
2018-06-12 10:30:44.581450+0300 App[12894:2931410] Progress: 62.652379 %
2018-06-12 10:30:44.753667+0300 App[12894:2931379] Progress: 99.009919 %
2018-06-12 10:30:44.756178+0300 App[12894:2931379] Progress: 100.000000 %
2018-06-12 10:30:44.859165+0300 App[12894:2931410] Progress: 100.000000 %
2018-06-12 10:30:44.884380+0300 App[12894:2931279] Updating button image
2018-06-12 10:30:44.884484+0300 App[12894:2931279] Updating button image
2018-06-12 10:30:44.884550+0300 App[12894:2931279] Updating button image
2018-06-12 10:30:44.884600+0300 App[12894:2931279] Updating button image
2018-06-12 10:30:44.884646+0300 App[12894:2931279] Updating button image
2018-06-12 10:30:44.884693+0300 App[12894:2931279] Updating button image
2018-06-12 10:30:44.884745+0300 App[12894:2931279] Updating button image
2018-06-12 10:30:44.884797+0300 App[12894:2931279] Updating button image
2018-06-12 10:30:44.884843+0300 App[12894:2931279] Updating button image

【问题讨论】:

  • 您是否尝试将 UIGraphicsBeginImageContextWithOptions 代码放在主队列块中?
  • 我试图将整个progressHandler放到主队列中,但同样的情况,一旦下载完整图像,主线程就会被执行。
  • 看这个,这可能会有所帮助nshipster.com/phimagemanager
  • 谢谢,看到了。不幸的是,我仍然面临下载完成后启动所有主线程活动的问题;在他们的示例中似乎并非如此。

标签: ios objective-c multithreading uiimage phasset


【解决方案1】:

问题来自我需要设置options.synchronous = NO的选项

【讨论】:

    猜你喜欢
    • 2019-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多