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

0

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];
}
于 2013-12-10T00:01:28.457 に答える