Search code examples
iosios6uiviewuiimagepickercontrollerhud

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

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.

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];
}

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?


Solution

  • 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];
    }