自定义相机视图与拆分视图

时间:2015-02-19 11:21:29

标签: ios camera uiimage uisplitview

我创建了许多布局,包括单帧到多帧。我们应该选择任何一个布局,选择相机应该出现拍照,帧将在拆分视图中。如果我们选择一个3帧的布局,我们正在采取选择第二帧,第一和第三相机不应该应用。我也看到AVCam示例代码,但没有得到正确的一个。如何使用分割图像自定义相机。希望有人帮助。

1 个答案:

答案 0 :(得分:1)

您好我通过AVFoundation框架得到了我的答案,我搜索了很多网站以及AVCam苹果示例。通过拍摄不同的视图和imageViews,我明白了在哪里使用相机预览

 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 = [SecondViewController 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(), ^{
       [[(AVCaptureVideoPreviewLayer *)[[self previewView] layer] connection] setVideoOrientation:(AVCaptureVideoOrientation)[self interfaceOrientation]];
        });
    }

    AVCaptureDevice *audioDevice = [[AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio] firstObject];
    AVCaptureDeviceInput *audioDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:&error];

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

    if ([session canAddInput:audioDeviceInput])
    {
        [session addInput:audioDeviceInput];
    }

    AVCaptureMovieFileOutput *movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
    if ([session canAddOutput:movieFileOutput])
    {
        [session addOutput:movieFileOutput];
        AVCaptureConnection *connection = [movieFileOutput connectionWithMediaType:AVMediaTypeVideo];
        if ([connection isVideoStabilizationSupported])
            [connection setEnablesVideoStabilizationWhenAvailable:YES];
        [self setMovieFileOutput:movieFileOutput];
    }

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

[[self session] startRunning];

通过使用此代码和一些重要的方法,如deviceWithMediaType,setFlashMode,我得到了我的解决方案