我正在从照片库导入多张照片,我想使用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");
}];
}
}
答案 0 :(得分: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");
}];
}
}
试试这个 因为每当MBProgressHUD的新对象获取并更新时,根据您的代码更新进度
答案 1 :(得分:0)
用户界面更新(即对进度条的更改)must be made on the main thread或您可能会遇到不可预测的副作用。
属性更改后,可能无法重绘用户界面元素
我相信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/[info count]
更新时打印出值来验证index
实际上是否按预期增加。
答案 2 :(得分:-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];
});
}
}
}
});
}