0

ココア タッチ AVFoundation フレームワークで静止画をキャプチャする必要がありますが、キャプチャした画像が引き伸ばされることがわかりました。次のように captureSession を構成しました。

[self setPreviewLayer:[[AVCaptureVideoPreviewLayer alloc] initWithSession:[self captureSession]]];
self.captureSession.sessionPreset=AVCaptureSessionPresetHigh;
[[self previewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];

そして、これらのコードを使用して画像をキャプチャします。

[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error)
{

    if(error)
        NSLog(@"error=%@",error);
    else
    {
        if (imageDataSampleBuffer != NULL) {
            NSData *imageData=[AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
            UIImage *image=[[UIImage alloc] initWithData:imageData];

            CGRect screenRect = [[UIScreen mainScreen] bounds];
            CGFloat screenWidth = screenRect.size.width;
            CGFloat screenHeight = screenRect.size.height;
            CGSize screenSize=CGSizeMake(screenWidth, screenHeight);
            NSLog(@"screen.width=%f,screen.height=%f",screenWidth,screenHeight);
            UIGraphicsBeginImageContextWithOptions(screenSize, NO, 0.0f);
            // This is where we resize captured image
            [(UIImage *)image drawInRect:CGRectMake(0, 0, screenWidth, screenHeight)];
            CGSize imageSize=[ImageUtility getImageSize:image];
            NSLog(@"image.width=%f,image.height=%f",imageSize.width,imageSize.height);
            // Save the results directly to the image view property
            UIImage *toSaveImage=UIGraphicsGetImageFromCurrentImageContext();
            imageSize=[ImageUtility getImageSize:toSaveImage];
            NSLog(@"combine.width=%f,combine.height=%f",imageSize.width,imageSize.height);
            UIGraphicsEndImageContext();
            UIImageWriteToSavedPhotosAlbum(toSaveImage, nil, nil, nil);
        }
    }
        }];
}

この結果出力:

013-08-29 09:30:06.182 WatermarkCamera[6882:907]        screen.width=320.000000,screen.height=480.000000
2013-08-29 09:30:07.208 WatermarkCamera[6882:907] image.width=1280.000000,image.height=720.000000
2013-08-29 09:30:07.238 WatermarkCamera[6882:907] combine.width=640.000000,combine.height=960.000000

結果の出力画像が引き伸ばされました。引き伸ばされた画像を修正するにはどうすればよいですか?ご意見をお聞かせください。

4

1 に答える 1

0

画面のサイズで画像を描くのではなく、画像のサイズで画像を描くべきだと思います。画像を新しい幅にスケーリングする新しい方法を定義しました

+(UIImage*)imageWithImage: (UIImage*) sourceImage scaledToWidth: (float) i_width;

今、私はこのメソッドを呼び出しました

UIImage *newImage=[ImageUtility imageWithImage:image scaledToWidth:screenWidth];
[newImage drawInRect:CGRectMake(0, 0, newImage.size.width, newImage.size.height)];

最後に新しい画像をフォト アルバムに書き込みます。新しい画像は伸びません。

于 2013-08-30T03:09:30.120 に答える