iOS不同版本的自定义有所不同的,本文介绍iOS8下的自定义相机,所用语言为swift3+
//
// TGCameraVCForiOS8.swift
// TGPhotoPicker
//
// Created by targetcloud on 2017/8/11.
// Copyright © 2017年 targetcloud. All rights reserved.
//
import UIKit
import AVFoundation
import Photos
class TGCameraVCForiOS8: UIViewController {
var callbackPicutureData: ((Data?) -> ())?
fileprivate var device: AVCaptureDevice?
fileprivate lazy var session : AVCaptureSession = AVCaptureSession()
fileprivate lazy var previewLayer : AVCaptureVideoPreviewLayer = AVCaptureVideoPreviewLayer(session: self.session)
fileprivate lazy var imageOutput : AVCaptureStillImageOutput = AVCaptureStillImageOutput()
fileprivate var input : AVCaptureDeviceInput?
fileprivate var showImageContainerView: UIView?
fileprivate var showImageView: UIImageView?
fileprivate var flashMode: AVCaptureFlashMode = .auto
fileprivate var picData: Data?
fileprivate var image: UIImage?
fileprivate lazy var takeButton: UIButton = {
let takeButton = UIButton(frame: CGRect(x: 0, y: 0, width: TGPhotoPickerConfig.shared.takeWH, height: TGPhotoPickerConfig.shared.takeWH))
takeButton.center = CGPoint(x: UIScreen.main.bounds.width / 2, y: UIScreen.main.bounds.height - TGPhotoPickerConfig.shared.buttonEdge.bottom)
takeButton.setImage(UIImage.size(width: TGPhotoPickerConfig.shared.takeWH, height: TGPhotoPickerConfig.shared.takeWH).border(width: 3).border(color: .white).color(.clear).corner(radius: TGPhotoPickerConfig.shared.takeWH / 2).image +
UIImage.size(width: TGPhotoPickerConfig.shared.takeWH - 10, height: TGPhotoPickerConfig.shared.takeWH - 10).color(UIColor(white: 0.95, alpha: 1) ).corner(radius: (TGPhotoPickerConfig.shared.takeWH - 10) / 2).image, for: .normal)
takeButton.setImage(UIImage.size(width: TGPhotoPickerConfig.shared.takeWH, height: TGPhotoPickerConfig.shared.takeWH).border(width: 3).border(color: .white).color(.clear).corner(radius: TGPhotoPickerConfig.shared.takeWH / 2).image +
UIImage.size(width: TGPhotoPickerConfig.shared.takeWH - 10, height: TGPhotoPickerConfig.shared.takeWH - 10).color(UIColor(white: 0.8, alpha: 1) ).corner(radius: (TGPhotoPickerConfig.shared.takeWH - 10) / 2).image, for: .highlighted)
takeButton.addTarget(self, action: #selector(takePhotoAction), for: .touchUpInside)
return takeButton
}()
fileprivate lazy var cameraChangeButton: UIButton = {
let cameraChangeButton = UIButton(frame: CGRect(x: 0, y: 0, width: TGPhotoPickerConfig.shared.takeWH * 0.6, height: TGPhotoPickerConfig.shared.takeWH * 0.6))
cameraChangeButton.setImage(TGPhotoPickerConfig.getImageNo2x3xSuffix("camera"), for: .normal)
cameraChangeButton.center = CGPoint(x: UIScreen.main.bounds.width - TGPhotoPickerConfig.shared.buttonEdge.right, y: self.takeButton.center.y)
cameraChangeButton.addTarget(self, action: #selector(changeCameraPositionAction), for: .touchUpInside)
cameraChangeButton.contentMode = .scaleAspectFit
return cameraChangeButton
}()
fileprivate lazy var flashButton: UIButton = {
let flashChangeButton = UIButton(frame: CGRect(x: 0, y: 0, width: TGPhotoPickerConfig.shared.takeWH * 0.5, height: TGPhotoPickerConfig.shared.takeWH * 0.5))
flashChangeButton.center = CGPoint(x: self.cameraChangeButton.center.x, y: TGPhotoPickerConfig.shared.buttonEdge.top)
flashChangeButton.setImage(TGPhotoPickerConfig.getImageNo2x3xSuffix("flashauto"), for: .normal)
flashChangeButton.addTarget(self, action: #selector(flashChangeAction), for: .touchUpInside)
flashChangeButton.contentMode = .scaleAspectFit
return flashChangeButton
}()
@objc fileprivate func flashChangeAction(){
gu