重要:私が使用した場合:session.sessionPreset = AVCaptureSessionPresetHigh;
私のプレビューイメージは伸びていません!!写真をデバイスに保存するとUIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
画像は正常で、プレビューでしか伸ばされません。キャプチャした写真をAVCaptureSessionで伸ばしますsessionPreset = AVCaptureSessionPresetPhoto
AVFoundationを使用して写真を撮影しています。
session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetHigh;
CALayer *viewLayer = vImagePreview.layer;
NSLog(@"viewLayer = %@", viewLayer);
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
captureVideoPreviewLayer.frame = vImagePreview.bounds;
[vImagePreview.layer addSublayer:captureVideoPreviewLayer];
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (!input) {
// Handle the error appropriately.
NSLog(@"ERROR: trying to open camera: %@", error);
}
[session addInput:input];
stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
[stillImageOutput setOutputSettings:outputSettings];
[session addOutput:stillImageOutput];
は私がAVCaptureSessionPresetPhoto
にsessionPresetを設定します。
session.sessionPreset = AVCaptureSessionPresetPhoto;
私のキャプチャ方法:
- (void) animateUpTheImageWithImage:(UIImage*)theImage{
UIView* preview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height/*426*/)];
CALayer *previewLayer = preview.layer;
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
captureVideoPreviewLayer.frame = previewLayer.frame;
[previewLayer addSublayer:captureVideoPreviewLayer];
captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspect;
[self addSubview:preview];
}
そして結果:私が撮影した写真のプレビューを追加
-(void)captureNow {
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in stillImageOutput.connections)
{
for (AVCaptureInputPort *port in [connection inputPorts])
{
if ([[port mediaType] isEqual:AVMediaTypeVideo])
{
videoConnection = connection;
break;
}
}
if (videoConnection)
{
break;
}
}
NSLog(@"about to request a capture from: %@", stillImageOutput);
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
{
CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
if (exifAttachments)
{
// Do something with the attachments.
NSLog(@"attachements: %@", exifAttachments);
} else {
NSLog(@"no attachments");
}
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
UIImage *image = [[UIImage alloc] initWithData:imageData];
NSLog(@"%@",NSStringFromCGSize(image.size));
[self animateUpTheImageWithImage:image];
}];
}
は私のキャプチャされたイメージが引き伸ばされる!
保存された写真が引き伸ばされていない場合、AVCaptureとはまったく関係ありません。あなたはそれについて考えましたか? –
こんにちは@incmikoあなたはこれを理解したことがありますか?私は今、同じ問題を抱えています。一日中作業していて、それを修正する方法がまだ見つかりませんでした。 – user3117509
私は私の質問に答え、それを下に見てください。私はそれがあなたの問題を解決することを願っています。 – incmiko