I'm using AVCam to capture image, but now I want to show square camera and take square image. How I can do it?
Thank you very much!
I'm using AVCam to capture image, but now I want to show square camera and take square image. How I can do it?
Thank you very much!
Those require two separate techniques.
It's important to understand the distinction between the two.
The first, modifying the video preview layer, requires adjusting the video gravity. Note that this can be changed as follows, in the context of full screen:
[Swift 3]
previewView.videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
Essentially, you are modifying the confines of your viewfinder. However, you are not providing any effect on your captured photo.
Now, for your situation of filling to a square you are required to define layer bounds then use AVLayerVideoGravityResize
.
As for modifying the captured photo:
My method is simply taking the returned image data, converting it to a UIImage, then performing a crop operation. The fact of the matter is, your captured photo will always be standard iPhone resolution and aspect ratio. You'll have to take the image data and convert it at some point in order to remove the bounds.
In truth, I no longer do this locally because it's a bother. If you're communicating with a REST api, I'd consider performing crop operations server-side--it's just as seamless and less overhead for the user. Of course, that may not suit your situation.