IOS development Note 17: Custom Camera photo, ios17
Previously, I used an AVFoundation custom camera to take photos and video-related things. Why do I need to customize it? It mainly provides a more personalized interaction design that conforms to the app topic. For videos, it is also convenient to provide more rich and interesting functions. Some time ago, I sorted out the functions of the Photo Section, which is mainly divided into the following five parts:
1. initialize, establish a session, and obtain the camera
In AVCaptureSessionPresetPhoto mode, the output image resolution is consistent with that of the System camera.
Add the rear camera and image output (by default, the rear camera is used for shooting)
2. embed the Real-Time preview Layer
Get the real-time preview screen and add a gesture. By default, the focus is set at the center of the screen and the focus box is displayed. The system prompts you to click the screen to adjust the focus and automatic exposure.
3. Related settings
Focus and Automatic Exposure Adjustment
Flashlight settings
Switch between the front camera and rear camera. The _ isFrontCameraInput identifier will be used when the final output is processed and the photos taken by the front camera need to be rotated 180 degrees.
4. Take Photos
Note the following:
(1) When you click the photo button on the view layer, You need to transmit the device direction information during the shooting. When you output the photo, the image will be rotated according to the device direction, make the final output result consistent with that of the System camera.
(Note: currently, the system uses the CMMotionManager sensor to obtain the device direction. In fact, you can also use the photo metadata to obtain the direction information for rotation)
(2) added the anti-shake mode, supported by iOS8 and later systems.
(3) obtain the metadata of the photo
(4) determine the photo cropping range. The cropping range is the preview area. By default, the system takes a full screen photo, but the preview area is not full screen in the uidesign. To ensure what you see is what you get, you need to crop the photo you have taken,
The preview area on iPhone 4 is a screen width area (considering the device size, iPhone 4 is designed separately). On other models, the preview area is a aspect ratio of (the width is equal to the screen width).
(5) image cropping and rotating processing, cropping method reference: https://github.com/mbcharbonneau/UIImage-Categories
Rotate the photo Based on the device direction information during the shooting. The front camera takes the photo and then rotates it for 180 degrees separately.
5. View layer UI and Interaction
The specific interaction design depends on the actual situation.
Note the following:
(1) When you go to the photo page, enable sensor monitoring to conveniently obtain the device direction information during shooting. If you destroy the device, you need to disable it.
(2) When switching the flashlight effect (On/Off/automatic), remember to save the selected settings and restore the settings when taking the next photo (consistent with the system camera behavior)