代码之家  ›  专栏  ›  技术社区  ›  Laurent Crivello

在progressHandler(iOS)中强制执行主队列

  •  0
  • Laurent Crivello  · 技术社区  · 8 年前

    但是,进程处理程序加载在后台队列中,我需要刷新主线程上的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
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Laurent Crivello    8 年前

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