显示HUD,同时图像缩小尺寸 [英] Showing a HUD whilst an image is downscaled in size
问题描述
我有一个应用程序,用户使用相机拍摄照片,然后选择使用照片。调用以下方法:
I have an app where the user takes a photo using the Camera and then chooses to Use the photo. The following method is called:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
在这个方法中,我检查 NSData
的长度如果数据(Kb)太大,请调整图像并调整实际图像大小,然后再次检查。这样我只会缩小少量以保持最高质量/尺寸的图像而不是特定尺寸w / h。
Within this method I check the NSData
length of the image and resize the actual image if the data (Kb) size is too large, then check again. This way I only scale down small amounts to keep the highest quality/sized image rather than a specific size w/h.
问题
我试图在图像缩放发生时向用户显示HUD。 HUD目前没有显示,这就是我的尝试。
Question I am trying to display a HUD to the user whilst the 'image scaling' is occurring. The HUD does not show at the moment, this is what I have tried.
// 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更新,我是否应该考虑在这里进行线程化。我不确定何时确定某些部分是否应该在不同的线程上?
I am adding the HUD to self.view but it does not show? Should I possibly think about threading here also, should the image scaling be completed on a background thread and the HUD updates on the main. I am unsure when to determine if certain parts should be on different threads?
推荐答案
在这样的后台线程中调用缩放方法:
Call the scaling method in the background thread like this :
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];
}
这篇关于显示HUD,同时图像缩小尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!