How to use the camera view in GoCoder SDK for iOS

Learn how to use the WZCameraPreview class UI component of the Wowza GoCoder™ SDK for iOS to display a preview of the active camera.

Contents


Display the camera preview
Detect the cameras available
Set the preview frame size
Set the crop and scale behavior

Display the camera preview


Use the cameraView property in the WowzaGoCoder class to specify the UIViewController-based view for the camera preview. Use the startPreview and stopPreview methods, respectively, to start and stop the camera preview display.

//
// Demonstrates typical usage within the implementation of a UIViewController-based subclass
//
- (void) viewDidLoad {
   [super viewDidLoad];

   // ... view initialization code ...

  // Initialize the SDK
  [WowzaGoCoder registerLicenseKey:SDKSampleAppLicenseKey];
  self.goCoder = [WowzaGoCoder sharedInstance];

  // Assign the camera view
  self.goCoder.cameraView = self.view;

  // Start the camera preview
  self.goCoderCameraPreview = self.goCoder.cameraPreview;
  [self.goCoderCameraPreview startPreview];

  // ... addt'l view initialization code ...

Detect the cameras available


The cameras property is an array of WZCamera objects that can be used to access each of the device cameras.

// Enable the switch camera button if more than one camera is available
self.switchCameraButton.enabled = self.goCoderCameraPreview.cameras.count > 1;

Set the active camera

Use the camera property to specify the active camera and the switchCamera method to switch between cameras when multiple cameras are available.

// Set the active camera to the front camera if it is not already active
if (self.goCoderCameraPreview.camera.direction != WZCameraDirectionFront)
    [self.goCoderCameraPreview.switchCamera];
}

Set the preview frame size


Use the config property to set the frame size for the camera preview.

Note: If you specify a frame size that the active camera doesn't support, the closest supported frame size is used.

// Set the camera preview to 720p
WowzaConfig previewConfig = self.goCoderCameraPreview.config;
[previewConfig loadPreset:WZFrameSizePreset1280x720];
self.goCoderCameraPreview.config = previewConfig;

Set the crop and scale behavior

Use the previewGravity property to specify how the camera preview should be displayed when the active frame size or aspect ratio of the camera does not match the view. The following modes are available:

WZCameraPreviewGravityResizeAspect Scale the camera preview to fit within the view surface. Note that letterboxing may be applied to maintain the proper aspect ratio.
WZCameraPreviewGravityResizeAspectFill Scale the camera preview to fill the entire view surface. Note that the preview may be cropped to maintain the proper aspect ratio.
WZCameraPreviewGravityResize Tretch the camera preview to fit entirely within the view surface.

Use the following command to set the previewGravity mode.

// Set the cropping mode so that the full frame is displayed, potentially cropped
self.goCoderCameraPreview.previewGravity = WZCameraPreviewGravityResizeAspect;

If you're having problems or want to discuss this article, post in our forum.