2016-03-01 60 views
0

我从照片库导入多张照片,我想用MBProgressHUD来显示进度。我正在使用QBImagePickerController导入照片。我的照片已成功导入。但是MBProgressHUD中的进度条未更新。下面是我的代码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"); 
    }]; 
    } 
} 
+0

是否'MBProgressHUD'成功地隐藏完成后? –

+0

是的,它确实@Mike – kathrineshineshine

回答

-1

试试看看这个代码。将您的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]; 
       }); 
      } 
     } 
    } 
}); 

}

+0

谢谢。这一个真的为我工作:) – kathrineshineshine

+0

@ kathrineshineshine看到我的答案理解**为什么**这[code-only answer](http://meta.stackexchange.com/a/148274)的作品。花几分钟时间学习这一点非常重要,这样您就不会在一个月内提出重复的问题(并浪费您自己的时间)了解不同的UI元素。 –

0

声明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"); 
    }]; 
} 
} 

试试这个 因为按照你的代码当u更新进度MBProgressHUD的每一次新的对象获取和更新

+0

谢谢你的回复。我已经完成了,但没有成功 – kathrineshineshine

0

UI更新(进度条即改变)must be made on the main thread或者你可以从不可预知的一侧患效果。你的属性修改

的用户界面元素可能不重绘我相信ALAssetsLibrary.assetForURL自动在后台线程中发生。为了解决这个问题,你想使用一个solution,让你在主线程上运行UI更新。在这种情况下,你的用户界面的更新将是此行

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

所以尝试这样的事:

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

此外,你应该确认++index/[info count]实际上是增加的,你指望它通过打印值了每次更新index