2

I created AVCaptureVideoPreviewLayer in portrait mode, I want to know how to appears it in 16:9 ration(landscape aspect ratio) while holding in portrait mode

I tried:

1.Giving camera PreviewLayer 16:9 size but it appears as zoomed

2.I tried using videoPreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.landscape here the resolution is perfect but it appears as rotate image (landscape camera)

I want to tilt the camera to portrait with landscape aspect ratio or portrait camera with landscape aspectRatio

Community
  • 1
  • 1
dev
  • 87
  • 1
  • 5

1 Answers1

0

calculate aspect ratio and then scale the image accordingly..

func imageWithImage (sourceImage:UIImage, scaledToWidth: CGFloat) -> UIImage {
    let oldWidth = sourceImage.size.width
    let scaleFactor = scaledToWidth / oldWidth

    let newHeight = sourceImage.size.height * scaleFactor
    let newWidth = oldWidth * scaleFactor

    UIGraphicsBeginImageContext(CGSize(width:newWidth, height:newHeight))
    sourceImage.draw(in: CGRect(x:0, y:0, width:newWidth, height:newHeight))
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage!
}
junaid
  • 193
  • 1
  • 16
  • Thanks for answering but i trying for live camera using AVCaptureSession to appear in AVCaptureVideoPreviewLayer which is of landscape resolution (horizontal frame 16:9 ration) while holding in portrait mode. – dev Aug 03 '18 at 12:03