iPad拍摄16:9照片

时间:2012-12-13 16:04:42

标签: objective-c ios ipad cocoa-touch

我正在iOS上构建一个原型应用程序,我正在蚕食一些Apple示例代码来实现它(薄冰,我知道 - 这段代码使用goto语句:\)。我正在使用Session 520中的AVCam项目 - Camera Capture中的新功能。我不需要视频捕捉功能,只需要静态照片。

设备输入和输出如此设置:

    // Init the device inputs
    AVCaptureDeviceInput *newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self backFacingCamera] error:nil];
    AVCaptureDeviceInput *newAudioInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self audioDevice] error:nil];


    // Setup the still image file output
    AVCaptureStillImageOutput *newStillImageOutput = [[AVCaptureStillImageOutput alloc] init];
    NSDictionary *outputSettings = @{AVVideoCodecKey: AVVideoCodecJPEG};
    [newStillImageOutput setOutputSettings:outputSettings];


    // Create session (use default AVCaptureSessionPresetHigh)
    AVCaptureSession *newCaptureSession = [[AVCaptureSession alloc] init];


    // Add inputs and output to the capture session
    if ([newCaptureSession canAddInput:newVideoInput]) {
        [newCaptureSession addInput:newVideoInput];
    }
    if ([newCaptureSession canAddInput:newAudioInput]) {
        [newCaptureSession addInput:newAudioInput];
    }
    if ([newCaptureSession canAddOutput:newStillImageOutput]) {
        [newCaptureSession addOutput:newStillImageOutput];
    }

    [self setStillImageOutput:newStillImageOutput];
    [self setVideoInput:newVideoInput];
    [self setAudioInput:newAudioInput];
    [self setSession:newCaptureSession];

这是我点按快门按钮时调用的方法:

- (void) captureStillImage
{
    AVCaptureConnection *stillImageConnection = [[self stillImageOutput] connectionWithMediaType:AVMediaTypeVideo];
    if ([stillImageConnection isVideoOrientationSupported])
        [stillImageConnection setVideoOrientation:orientation];

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

                 ALAssetsLibraryWriteImageCompletionBlock completionBlock = ^(NSURL *assetURL, NSError *error) {
                     if (error)
                     {
                         if ([[self delegate] respondsToSelector:@selector(captureManager:didFailWithError:)])
                         {
                             [[self delegate] captureManager:self didFailWithError:error];
                         }
                     }
                 };

                 if (imageDataSampleBuffer != NULL)
                 {
                     NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
                     ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

                     UIImage *image = [[UIImage alloc] initWithData:imageData];

                     if ([self.delegate respondsToSelector:@selector(captureManagerCapturedImage:)])
                     {
                         dispatch_async(dispatch_get_main_queue(), ^{
                             [self.delegate captureManagerCapturedImage:image];
                         });
                     }

                     [library writeImageToSavedPhotosAlbum:[image CGImage]
                                               orientation:(ALAssetOrientation)[image imageOrientation]
                                           completionBlock:completionBlock];

                 }
                 else
                 {
                     completionBlock(nil, error);
                 }

                 if ([[self delegate] respondsToSelector:@selector(captureManagerStillImageCaptured:)])
                 {
                     [[self delegate] captureManagerStillImageCaptured:self];
                 }
             }];
}

此代码成功捕获图像并将其保存到库中。然而,在我工作的某个时刻,它从捕获500万像素4:3图像变为捕获1920x1080 16:9图像。我无法找到指定宽高比的任何地方,并且我没有更改与摄像机配置,捕获会话或捕获连接相关的任何代码。为什么我的相机开始拍摄16:9照片?

更新:我刚刚重新运行了Apple的原始示例代码,它似乎也可以保存直接从视频中捕获的16:9图像。很可能我以前疯了,或者我用Camera.app拍了一张测试照片并且看着它。所以我真正的问题是,如何在拍摄时从屏幕上的相机显示实时信息,并拍摄全分辨率照片。我无法使用UIImagePickerController,因为我需要能够在实时相机Feed之上叠加内容。

更新2:我能够通过丢弃我正在使用的AVCapture代码来解决这个问题。事实证明,UIImagePickerController可以满足我的需求。我没有意识到你可以覆盖自定义控件 - 我认为它会占用整个屏幕,直到你拍完照片为止。

1 个答案:

答案 0 :(得分:1)

如果您从视频源捕获帧,则最终得分为16:9。从视频源捕捉帧并拍照是不同的事情。