0

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

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

4

2 回答 2

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

于 2013-12-11T20:27:03.957 回答
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];
}
于 2013-12-10T00:01:28.457 回答