显示HUD,同时图像缩小尺寸

时间:2013-12-09 23:47:33

标签: ios ios6 uiview uiimagepickercontroller hud

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

- (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更新,我是否应该考虑在这里进行线程化。我不确定何时确定某些部件是否应该在不同的线程上?

2 个答案:

答案 0 :(得分:1)

您只调整一张图片吗?还是几个?如果做一个,你可以:

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调整为您想要的任何值。坦率地说,考虑到这一点的全部意义在于您希望将较大的图像缩小,您可能不希望同时运行太多(因为内存使用问题),但这是一个选项。

答案 1 :(得分:0)

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

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