Search code examples
ioscameraavfoundationvideo-recording

how to capture a video in specific part rather than full screen in iOS


I am capturing a video in my IOS app using AVFoundation. i am able to record the video and able to playback also.

But my problem is that i am showing the capturing video in a view which is around 200 points height.so i expected the video would be recorded in the same dimensions.but when i playback the video its showing that the whole screen has been recorded.

so i want to know is there any way to record the camera preview which was visible to user only.And any help should be appreciated.

the screenshots: (While Recording) (while playback)


Solution

  • You cannot think of video resolution in terms of UIView dimensions (or even screen size, for that matter). The camera is going to record at a certain resolution depending on how you set up the AVCaptureSession. For instance, you can change the video quality by setting the session preset via:

    [self.captureSession setSessionPreset:AVCaptureSessionPreset640x480]
    

    (It is already set by default to the highest setting.)

    Now, when you play the video back, you do have a bit of control over how it is presented. For instance, if you want to play it in a smaller view (who's layer is of type AVPlayerLayer), you can set the video gravity via:

    AVCaptureVideoPreviewLayer *previewLayer = (AVCaptureVideoPreviewLayer*)self.previewView.layer;
    [previewLayer setVideoGravity:AVLayerVideoGravityResizeAspect];
    

    And depending on what you pass for the gravity parameter, you will get different results.

    Hopefully this helps. Your question is a little unclear as it seems like you want the camera to only record a certain amount of it's input, but you'd have to put your hand over part of the lens to accomplish that ;)