AVCam保存全屏捕获的图像

时间:2014-09-05 11:45:04

标签: ios objective-c avcapturesession avcam

我正在使用Apple制作的AVCam作为自定义相机视图。老实说,如果你第一次看到它,就不能简单地理解班级AVCamViewController里发生了什么。

现在我很感兴趣他们如何设置捕获图像的帧。我试图找到一些名人或类似的东西,但我没有找到任何。

我在Google搜索并在此处找到答案AVCam not in fullscreen

但是当我实现该解决方案时,我才刚刚意识到它只是制作了与我的视图相同大小的实时相机预览图层,但是当应用程序将图像保存在图库中的方法- (IBAction)snapStillImage:(id)sender时,图像仍然带有2个条纹从左到右。

我的问题是如何删除此条纹或源代码中的哪一行设置了这个东西?

另外作为额外的子问题我如何设置类型创建只是照片,因为该应用程序请求我“麦克风设置”,我不需要它只需要拍一张照片,就是这样。

Apple源代码会将图像保存到照片库。

- (IBAction)snapStillImage:(id)sender
{
    dispatch_async([self sessionQueue], ^{
        // Update the orientation on the still image output video connection before capturing.
        [[[self stillImageOutput] connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:[[(AVCaptureVideoPreviewLayer *)[[self previewView] layer] connection] videoOrientation]];

        // Flash set to Auto for Still Capture
        [AVCamViewController setFlashMode:AVCaptureFlashModeAuto forDevice:[[self videoDeviceInput] device]];

        // Capture a still image.
        [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:[[self stillImageOutput] connectionWithMediaType:AVMediaTypeVideo] completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {

            if (imageDataSampleBuffer)
            {
                NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];

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

                UIImage *proccessingImage = [SAPAdjustImageHelper adjustImage:image];

                NSNumber *email_id = [SAPCoreDataEmailHelper currentEmailId];

                [SAPFileManagerHelper addImage:proccessingImage toFolderWithEmailId:email_id];
            }
        }];
    });
}

2 个答案:

答案 0 :(得分:5)

您是否正在设置会话预设?

您可以将会话与AVCaptureSessionPresetPhoto中设置的会话预设一起使用。

对于另一个子问题:您只需要添加AVCaptureStillImageOutput输出。

如何设置会话预设?

[session setSessionPreset:AVCaptureSessionPresetPhoto];

如何将会话配置为仅使用StillImageOutput拍摄照片?

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Create the AVCaptureSession
    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    [self setSession:session];

    // Setup the preview view
    [[self previewView] setSession:session];

    // Setup the session Preset          
    [session setSessionPreset:AVCaptureSessionPresetPhoto];

    // Check for device authorization
    [self checkDeviceAuthorizationStatus];

    dispatch_queue_t sessionQueue = dispatch_queue_create("session queue", DISPATCH_QUEUE_SERIAL);
    [self setSessionQueue:sessionQueue];

    dispatch_async(sessionQueue, ^{
        //[self setBackgroundRecordingID:UIBackgroundTaskInvalid];

        NSError *error = nil;

        AVCaptureDevice *videoDevice = [AVCamViewController deviceWithMediaType:AVMediaTypeVideo preferringPosition:AVCaptureDevicePositionBack];
        AVCaptureDeviceInput *videoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];

        if (error)
        {
            NSLog(@"%@", error);
        }

        if ([session canAddInput:videoDeviceInput])
        {
            [session addInput:videoDeviceInput];
            [self setVideoDeviceInput:videoDeviceInput];

            dispatch_async(dispatch_get_main_queue(), ^{
                // Why are we dispatching this to the main queue?
                // Because AVCaptureVideoPreviewLayer is the backing layer for AVCamPreviewView and UIView can only be manipulated on main thread.
                // Note: As an exception to the above rule, it is not necessary to serialize video orientation changes on the AVCaptureVideoPreviewLayer’s connection with other session manipulation.

                [[(AVCaptureVideoPreviewLayer *)[[self previewView] layer] connection] setVideoOrientation:(AVCaptureVideoOrientation)[self interfaceOrientation]];
            });
        }

AVCaptureStillImageOutput *stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
        if ([session canAddOutput:stillImageOutput])
        {
            [stillImageOutput setOutputSettings:@{AVVideoCodecKey : AVVideoCodecJPEG}];
            [session addOutput:stillImageOutput];
            [self setStillImageOutput:stillImageOutput];
        }
    });
}

答案 1 :(得分:0)

需要使用黑色字段来保持宽高比,如果要避开它们,则应更改videoGravity中的AVCaptureVideoPreviewLayercontentMode中的UIImageView显示捕获的图像 这是一种预期的行为,不了解您的担忧。