2013-10-04 25 views
6

重要:私が使用した場合: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]; 


    }]; 

} 

は私のキャプチャされたイメージが引き伸ばされる!

Capturing camera preview

Captured image preview

+0

保存された写真が引き伸ばされていない場合、AVCaptureとはまったく関係ありません。あなたはそれについて考えましたか? –

+0

こんにちは@incmikoあなたはこれを理解したことがありますか?私は今、同じ問題を抱えています。一日中作業していて、それを修正する方法がまだ見つかりませんでした。 – user3117509

+0

私は私の質問に答え、それを下に見てください。私はそれがあなたの問題を解決することを願っています。 – incmiko

答えて

14

を使用してみてください。ここで私が今使っているどのようなコードであり、それは正常に動作(S)

session = [[AVCaptureSession alloc] init]; 
    session.sessionPreset = AVCaptureSessionPresetHigh; 

    captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session]; 
    captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
    captureVideoPreviewLayer.frame = vImagePreview.bounds; 
    [vImagePreview.layer addSublayer:captureVideoPreviewLayer]; 

    device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 

...

stillImageOutput = [[AVCaptureStillImageOutput alloc] init]; 
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil]; 
[stillImageOutput setOutputSettings:outputSettings]; 
[session addOutput:stillImageOutput]; 

capturedImageView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.screenWidth,self.screenHeight)]; 
[self addSubview:capturedImageView]; 

を、重要な出力imagaView:

vImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.screenWidth,self.screenHeight)]; 
    [capturedImageView addSubview:vImage]; 
    vImage.autoresizingMask = (UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleWidth); 
    vImage.contentMode = UIViewContentModeScaleAspectFill; 
    vImage.image = theImage; 

いくつかの追加に関する情報:

カメラレイヤーはフルスクリーンでなければなりません(y座標は変更できますが、幅と高さはフルサイズでなければなりません)。 outputImageViewも同じでなければなりません。

私はこれらが誰かにとって有益な情報であることを願っています。

+0

vImageリンゴを使用していることを意味するAVCamはソースにこれを持っていませんか? –

0

だから私は私の問題を解決し[captureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

+0

これは何もしません、私のプレビューは同じです。 session.sessionPreset = AVCaptureSessionPresetHigh;これで正しく働きます。 session.sessionPreset = AVCaptureSessionPresetPhoto;プレビューが悪い – incmiko

+0

@incmiko他のすべてのタイプの 'AVLayerVideoGravity'変数を試しましたか? –

+0

私は毎回試しました – incmiko

0

キャプチャsessionPresetをAVCaptureSessionPresetiFrame960x540に変更します。それは私のためにうまくいく。

関連する問題