在图像尺寸缩小的同时显示HUD

我有一个应用程序,用户使用相机拍照,然后select使用照片。 以下方法被调用:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 

在这个方法中,我检查图像的NSData长度,如果数据(Kb)大小太大,则重新调整实际图像大小,然后再次检查。 这样我只能缩小量以保持最高的质量/尺寸的图像,而不是一个特定的大小w / h。

问题我正在尝试向用户显示HUD,同时发生“图像缩放”。 HUD目前没有显示,这是我所尝试过的。

 // Check if the image size is too large if ((imageData.length/1024) >= 1024) { MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; HUD.dimBackground = YES; HUD.labelText = NSLocalizedString(@"HUDHomeLoadingTableData", @"Home View Controller - Loading Table Data"); HUD.removeFromSuperViewOnHide = YES; while ((imageData.length/1024) >= 1024) { NSLog(@"While start - The imagedata size is currently: %f KB",roundf((imageData.length/1024))); // While the imageData is too large scale down the image // Get the current image size CGSize currentSize = CGSizeMake(image.size.width, image.size.height); // Resize the image image = [image resizedImage:CGSizeMake(roundf(((currentSize.width/100)*80)), roundf(((currentSize.height/100)*80))) interpolationQuality:kMESImageQuality]; // Pass the NSData out again imageData = UIImageJPEGRepresentation(image, kMESImageQuality); } [HUD hide:YES]; } 

我将HUD添加到self.view,但它不显示? 我是否应该考虑在这里进行线程化,如果图像缩放在后台线程上完成,并且HUD在主体上更新。 我不确定何时确定某些部分是否应该在不同的线程上?

在后台线程中调用缩放方法,如下所示:

 if ((imageData.length/1024) >= 1024) { self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; HUD.dimBackground = YES; HUD.labelText = NSLocalizedString(@"HUDHomeLoadingTableData", @"Home View Controller - Loading Table Data"); HUD.removeFromSuperViewOnHide = YES; self.scaledImageData = imageData; [self performSelectorInBackground:@selector(scaleDown:) withObject:imageData]; } -(void)scaleDown:(NSData*)imageData { while ((imageData.length/1024) >= 1024) { NSLog(@"While start - The imagedata size is currently: %f KB",roundf((imageData.length/1024))); // While the imageData is too large scale down the image // Get the current image size CGSize currentSize = CGSizeMake(image.size.width, image.size.height); // Resize the image image = [image resizedImage:CGSizeMake(roundf(((currentSize.width/100)*80)), roundf(((currentSize.height/100)*80))) interpolationQuality:kMESImageQuality]; // Pass the NSData out again self.scaledImageData = UIImageJPEGRepresentation(image, kMESImageQuality); } //hide the hud on main thread [self performSelectorOnMainThread:@selector(hideHUD) withObject:nil waitUntilDone:NO]; } -(void)hideHUD { [self.HUD hide:YES]; } 

你只调整一个图像? 还是几个? 如果做一个,你可以:

 MBProgressHUD *HUD = [self showHUD]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // do your image resizing here // when done, hide the HUD on the main queue dispatch_async(dispatch_get_main_queue(), ^{ [self hideHUD:HUD]; }); }); 

哪里

 - (MBProgressHUD *)showHUD { MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; HUD.dimBackground = YES; HUD.labelText = NSLocalizedString(@"HUDHomeLoadingTableData", @"Home View Controller - Loading Table Data"); HUD.removeFromSuperViewOnHide = YES; return HUD; } - (void)hideHUD:(MBProgressHUD *)HUD { [HUD hide:YES]; } 

如果调整一大堆图像的大小,您应该定义自己的队列来进行大小调整:

 MBProgressHUD *HUD = [self showHUD]; NSOperationQueue *resizeQueue = [[NSOperationQueue alloc] init]; resizeQueue.maxConcurrentOperationCount = 1; NSOperation *completeOperation = [NSBlockOperation blockOperationWithBlock:^{ //when done, hide the HUD (using the main queue) [[NSOperationQueue mainQueue] addOperationWithBlock:^{ [self hideHUD:HUD]; }]; }]; for (NSInteger i = 0; i < imageCount; i++) { NSOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ // do your image resizing here }]; // make the completion operation dependent upon each image resize operation [completionOperation addDependency:operation]; // queue the resize operation [resizeQueue addOperation:operation]; } // when all done, queue the operation that will remove the HUD [resizeQueue addOperation:completionOperation]; 

请注意,我假设您将一次一个地(连续地)执行它们,但是如果要同时执行这些操作,只需将maxConcurrentOperationCount调整为您想要的任何值即可。 坦率地说,考虑到这一点,你想要缩小图像大小,你可能不会想同时运行太多(因为内存使用的担忧),但它是一个选项。