Use an AVFoundation custom camera to take photos,

Source: Internet
Author: User

Use an AVFoundation custom camera to take photos,

You need to use the AVFoundation framework to customize the photo taking or video recording function. Currently, I only use the photo recording function, so I have recorded the usage of custom photos and video usage, it should be similar demo here: https://github.com/Phelthas/TEST_XMLCommon to take the photo process as an example, the Implementation mainly includes the following parts: 1, first to determine the user authorization: let authorizationStatus = AVCaptureDevice. authorizationStatusForMediaType (AVMediaTypeVideo) switch authorizationStatus {
Case. NotDetermined:
AVCaptureDevice. requestAccessForMediaType (AVMediaTypeVideo, completionHandler: {(granted: Bool)-> Void in
If granted {
Self. configureCamera ()
} Else {
Self. showErrorAlertView ()
}
})
Case. Authorized:
Self. configureCamera ()
Default:
Self. showErrorAlertView ()} 2. AVCaptureInput is the input end of the photo process. This can be a front camera. The rear camera or microphone must use public init (device: AVCaptureDevice !) Throws is obtained by initialization. AVCaptureDevice is the abstraction of the capture device defined by Apple. It uses public class func devicesWithMediaType (mediaType: String !) -> [AnyObject]! Method. This mediaType: String is also defined in AVMediaFormat. h. here, AVMediaTypeVideo 3 is used. AVCaptureOutputAVCaptureOutput is the output end of the abstract photo process, which can be images, videos, audios, etc., mainly including: AVCaptureFileOutput, AVCaptureStillImageOutput, expires, expires, and AVCaptureMovieFileOutput. AVCaptureFileOutput still indicates the output to the file. AVCaptureStillImageOutput 4 is used here, it requires an input: AVCaptureInput, an output: AVCaptureOutput, if session. canAddInput (cameraInput) {session. addInput (cameraInput)} if session. canAddInput (cameraInput) {session. addInput (cameraInput)} Then calls the startRunning method of the session to capture image information. AVCaptureVideoPreviewLayer is generally used for image preview. previewLayerpreviewLayer = AVCaptureVideoPreviewLayer (session: self. session) then set the frame of the layer and add the layer to the position where you want to display the preview. 5. You can also focus on the camera before calling the startRunning of the session, white Balance and other settings if let tempDevice = self. currentCameraDevice {try tempDevice. lockForConfiguration ()
If tempDevice. hasFlash {
TempDevice. flashMode = self. flashMode
}
If tempDevice. isFocusModeSupported (. AutoFocus ){
TempDevice. focusMode =. AutoFocus
}
If tempDevice. isWhiteBalanceModeSupported (. AutoWhiteBalance ){
TempDevice. whiteBalanceMode =. AutoWhiteBalance
}
TempDevice. unlockForConfiguration ()
} 6. To get an image to a specified device, call the stillImageOutput method to obtain the image. The method is as follows: let connection = self. stillImageOutput. connectionWithMediaType (AVMediaTypeVideo)
Self. stillImageOutput. captureStillImageAsynchronouslyFromConnection (connection) {[unowned self] (imageDataSampleBuffer: CMSampleBuffer !, Error)-> Void in if error = nil {// if session is used. photo preset, or explicitly set in the output settings of the device, you can get the data that has been compressed as JPEG. let imageData = AVCaptureStillImageOutput.jpeg StillImageNSDataRepresentation (imageDataSampleBuffer) // The sample buffer also contains metadata // let metadata: NSDictionary = CMCopyDictionaryOfAttachments (nil, imageDataSampleBuffer, CMAttachmentMode (kCMAttachmentMode_ShouldPropagate ))! If let image = UIImage (data: imageData) {// This image is the image obtained by the obtained photo. It can be saved to the album or modified here, what else do you want to do? // pay attention to the image resolution when the camera captures the image, which is not the layer size you set. If you need to crop the image, save it after cropping it}
} Else {
DLog ("error while capturing still image: \ (error )")
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.