Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities
Abstract
Usually realize the function of taking photos, are a search on the Internet, the whole copy and paste, claiming to be brainless. But when a photo is required in a different size and format (JPEG), you struggle with the photo. In fact, when setting photos, you can directly set the size and format of photos, with direct methods to meet the requirements, isn’t it better?
Before taking pictures, you need to create AVCaptureSession session object, if it’s just picture need to create AVCapturePhotoOutput object, preview the page displayed on AVCaptureVideoPreviewLayer objects of a class, The capturePhoto(with:, delegate:) function of the AVCapturePhotoOutput object is called when capturing a photo. If you are interested in this process, leave me a comment, I will comb through this process in detail, and then enter the focus of this issue.
Set the size of the picture
To set the size of an image, use the sessionPreset property, set during setting AVCaptureSession session configuration.
thensessionPreset
What do you do? With that much energy?
According to the Apple documentation, it is the default session that indicates the session is currently in use. This setting can affect the size of the video stream or image (in pixels, especially).
The size can be set and AVCaptureSession.Preset is one to one corresponding to the enumeration property, which is of interest and can be tracked in Xcode.
Two important points to note, here is height and then width, the image is outlined in red, and the size is in pixels.
It is easy to set the size of the photo you want (default is.high) :
captureSession.sessionPreset = .high
Copy the code
Format the image
Here is how to set the photo format to JPEG. In fact, it also supports other formats, here to tell the way, other formats can be viewed directly by following the code.
The function capturePhoto(with Settings: AVCapturePhotoSettings, delegate: AVCapturePhotoCaptureDelegate) to the incoming AVCapturePhotoSettings type of object, then set up on the inside I need photos of the format:
/ / verification equipment support format of one self. PhotoOutput. AvailablePhotoCodecTypes. The contains (AVVideoCodecType (rawValue: AVVideoCodecJPEG)) {// Set the image format photoSettings = AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecJPEG])}Copy the code
You then pass the photoSettings object into the capturePhoto function, and the photo you get is the one you formatted. There are many other formats in this list. For details, see the AVVideoCodecType type.
What is a mirror image?
By way of a quick mention, if you use a front-facing camera to capture photos in your project, you will find that the photos captured by the front-facing camera are the most inverted compared to the photos captured by the rear camera in the same environment. So how do you flip it over when you capture it?
Here you need to set isVideoMirrored to the connection of the output object. When set to true, the image captured will be flipped.
/ / output connection object if the let connection = self. PhotoOutput. Connection (with: Video) {/ / Settings are images, connection. The default is false isVideoMirrored = true}Copy the code
digression
Time is short, what you said may not be comprehensive, in the process of your implementation of any problems, please leave me a message in the comment section, I will reply as soon as possible