MBProgressHUD 不更新进度

MBProgressHUD not updating progress

我正在从照片库中导入多张照片,我想使用 MBProgressHUD 显示进度。我正在使用 QBImagePickerController 导入照片。我的照片导入成功。但是 MBProgressHUD 中的进度条没有更新。以下是我的代码

-(void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didSelectAssets:(NSArray *)assets {
   if (imagePickerController.filterType == QBImagePickerControllerFilterTypePhotos) {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];]

    // Set the determinate mode to show task progress.
    hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
    hud.delegate = self;

    hud.labelText = NSLocalizedString(@"Importing Photos", nil);
    hud.dimBackground = YES;
    hud.detailsLabelFont = [UIFont systemFontOfSize:12.0f];
    hud.detailsLabelText = NSLocalizedString(@"Please wait...", nil);
    hud.progress = 0.0f;

    [self importPhotosForArray:assets];
   }
    [self dismissImagePickerController];
}

 - (void) importPhotosForArray:(NSArray *)info {
       for (ALAsset *selectedImageAsset in info) {
       ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
    [assetsLibrary assetForURL:[selectedImageAsset defaultRepresentation].url resultBlock: ^(ALAsset *asset){
        ALAssetRepresentation *representation = [asset defaultRepresentation];
        CGImageRef imageRef = [representation fullResolutionImage];
        if (imageRef) {
            UIImage *image = [UIImage imageWithCGImage:imageRef];
            NSData *imageData = UIImageJPEGRepresentation(image, 1.0);

            // Create a file name for the image
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
            [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
            NSString *imageName = [NSString stringWithFormat:@"photo-%@.png", [dateFormatter stringFromDate:[NSDate date]]];
            // Now we get the full path to the file
            NSString *fullPathToFile = [self.folder.fullPath stringByAppendingPathComponent:imageName];
            // Write out the data.
            [imageData writeToFile:fullPathToFile atomically:NO];

            sleep(1.0);
            progress = ++index/[info count];

            [MBProgressHUD HUDForView:self.navigationController.view].progress = progress;

            if (progress >= 1.0) {
                [[MBProgressHUD HUDForView:self.navigationController.view] hide:YES];
                    [self reloadData];
            }
        }
    } failureBlock: ^(NSError *error){
        // Handle failure.
        NSLog(@"Failure");
    }];
   }
}

将 MBProgressHUD 声明为 属性 并在任何地方使用它。

@property (nonatomic, strong) MBProgressHUD *hud;

-(void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didSelectAssets:(NSArray *)assets {

    if (imagePickerController.filterType == QBImagePickerControllerFilterTypePhotos) {

            self.hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];]

           // Set the determinate mode to show task progress.
           self.hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
           self.hud.delegate = self;

           self.hud.labelText = NSLocalizedString(@"Importing Photos", nil);
           self.hud.dimBackground = YES;
           self.hud.detailsLabelFont = [UIFont systemFontOfSize:12.0f];
           self.hud.detailsLabelText = NSLocalizedString(@"Please wait...", nil);
           self.hud.progress = 0.0f;

           [self importPhotosForArray:assets];
        }
            [self dismissImagePickerController];
}

- (void) importPhotosForArray:(NSArray *)info {

   for (ALAsset *selectedImageAsset in info) {

          ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];

          [assetsLibrary assetForURL:[selectedImageAsset defaultRepresentation].url resultBlock: ^(ALAsset *asset){

          ALAssetRepresentation *representation = [asset defaultRepresentation];
          CGImageRef imageRef = [representation fullResolutionImage];

        if (imageRef) {

              UIImage *image = [UIImage imageWithCGImage:imageRef];
              NSData *imageData = UIImageJPEGRepresentation(image, 1.0);

        // Create a file name for the image
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
        [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
        NSString *imageName = [NSString stringWithFormat:@"photo-%@.png", [dateFormatter stringFromDate:[NSDate date]]];
        // Now we get the full path to the file
        NSString *fullPathToFile = [self.folder.fullPath stringByAppendingPathComponent:imageName];
        // Write out the data.
        [imageData writeToFile:fullPathToFile atomically:NO];

        sleep(1.0);
        progress = ++index/[info count];

        self.hud.progress = progress;

        if (progress >= 1.0) {

         [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];

                [self reloadData];
        }
    }
} failureBlock: ^(NSError *error){
    // Handle failure.
    NSLog(@"Failure");
  }];
}
}

试试这个 因为根据您的代码,每次 MBProgressHUD 的新对象获取和更新时您都会更新进度

UI 更新(即更改进度条)must be made on the main thread 否则您可能会遭受不可预测的副作用。

The user interface element may not be redrawn after your property changes

我相信 ALAssetsLibrary.assetForURL 是在后台线程中自动发生的。要解决这个问题,您需要使用 solution 来让您 运行 主线程上的 UI 更新。在这种情况下,您的 UI 更新将是这一行

[MBProgressHUD HUDForView:self.navigationController.view].progress = progress;

所以尝试这样的事情:

dispatch_async(dispatch_get_main_queue(), ^{
    [MBProgressHUD HUDForView:self.navigationController.view].progress = progress;
});

此外,您应该通过在每次 index 更新时打印出值来验证 ++index/[info count] 是否确实如您预期的那样增加。

试试这个代码。将您的 importPhotosForArray 方法替换为以下内容。

- (void) importPhotosForArray:(NSArray *)info {
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
    __block float progress = 0.0f;
    __block float index = 0.0f;

    for (ALAsset *selectedImageAsset in info) {
        ALAssetRepresentation *representation = [selectedImageAsset defaultRepresentation];
        CGImageRef imageRef = [representation fullResolutionImage];
        if (imageRef) {
            UIImage *image = [UIImage imageWithCGImage:imageRef];
            NSData *imageData = UIImagePNGRepresentation(image);

            // Create a file name for the image
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
            [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
            NSString *imageName = [NSString stringWithFormat:@"photo-%@.png", [dateFormatter stringFromDate:[NSDate date]]];
            // Now we get the full path to the file
            NSString *fullPathToFile = [self.folder.fullPath stringByAppendingPathComponent:imageName];
            // Write out the data.
            [imageData writeToFile:fullPathToFile atomically:YES];

            sleep(1);

            progress = ++index/[info count];

            dispatch_async(dispatch_get_main_queue(), ^{
                // Instead we could have also passed a reference to the HUD
                // to the HUD to myProgressTask as a method parameter.
                [MBProgressHUD HUDForView:self.navigationController.view].progress = progress;
            });

            if (progress >= 1.0) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    // Instead we could have also passed a reference to the HUD
                    // to the HUD to myProgressTask as a method parameter.
                    [[MBProgressHUD HUDForView:self.navigationController.view] hide:YES];
                    [self reloadData];
                });
            }
        }
    }
});

}