相机里面有的是图片更加个人需要去改动,
有什么可以帮助到你么你的可以联系我,980471259关于自定义相机,OC的也可以联系我..再给你们完美的相机,相机存所项目需要而自定义的
//
// CustomCameraViewController.swift
// Lease
//
// Created by Apple on 2017/3/10.
// Copyright © 2017年 Apple. All rights reserved.
//
import UIKit
import AVFoundation
import SnapKit
class CustomCameraViewController: UIViewController {
typealias getImage = (( _ image:UIImage) ->())?
var Preset = Int() // 通过上一个界面的参数,来判断调用的相机画质,一般的证件获取的是普通画质的照片,资料的画质是最高画质(更加手机型号的不同,画质可能也是不同的)
var array:[String]?
let captureSession = AVCaptureSession()
let stillImageOutput = AVCaptureStillImageOutput()
var previewLayer : AVCaptureVideoPreviewLayer?
// If we find a device we’ll store it here for later use
var captureDevice : AVCaptureDevice?
var image = UIImage()
var focusView = UIView()
var gtimage:getImage?
var takePhoto = false
let hdbtn = UIButton()
var cameraView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
if Preset == 1 || Preset == 2 || Preset == 3 {
captureSession.sessionPreset = AVCaptureSessionPresetHigh
}else{
captureSession.sessionPreset = AVCaptureSessionPresetPhoto
}
if let devices = AVCaptureDevice.devices() as? [AVCaptureDevice] {
// Loop through all the capture devices on this phone遍历所有捕获设备在这个电话
for device in devices {
// Make sure this particular device supports video确保这个特殊的设备支持视频
if (device.hasMediaType(AVMediaTypeVideo)) {
// Finally check the position and confirm we've got the back camera最后检查的位置并确认我们有相机
if(device.position == AVCaptureDevicePosition.back) {
captureDevice = device
if captureDevice != nil {
// print(“Capture device found(捕获设备发现)”)
beginSession()
}
}
}
}
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
cameraView.frame = CGRect(x:0,y:64,width:SCREEN_WIDTH,height:SCREEN_HEIGHT - 64)
self.view.addSubview(cameraView)
setUpUI()
}
func setUpUI(){
let v = UIView(frame:CGRect(x:0,y:SCREEN_HEIGHT - 71,width:SCREEN_WIDTH,height:71))
self.view.addSubview(v)
if Preset == 1 {
array = [“左上”,”右上”,”左下”,”右下”]
for i in 0..<4{
let imageView = UIImageView()
imageView.frame = CGRect(x:Int(i%2) * Int(SCREEN_WIDTH-20),y:200 * (i/2) + 100,width:20,height:20)
imageView.image = UIImage(named:array![i])
self.view.addSubview(imageView)
imageView.bringSubview(toFront: self.view)
}
}else if Preset == 2{
let imag = UIImageView()
imag.image = UIImage(named:”IDcard1”)
view.addSubview(imag)
imag.bringSubview(toFront: self.view)
imag.snp.makeConstraints({ (make) in
make.left.equalTo(5)
make.width.equalTo(SCREEN_WIDTH - 10)
make.height.equalTo(SCREEN_WIDTH * 0.618)
make.top.equalTo(100)
})
}else if Preset == 3{
let imag = UIImageView()
imag.image = UIImage(named:"perImage")
view.addSubview(imag)
imag.bringSubview(toFront: self.view)
imag.snp.makeConstraints({ (make) in
make.left.equalTo(5)
make.width.equalTo(SCREEN_WIDTH - 10)
make.height.equalTo(SCREEN_WIDTH * 0.618)
make.top.equalTo(100)
})
}
let btn = UIButton()
btn.setImage(UIImage(named:"photograph"), for: .normal)
btn.setImage(UIImage(named:"photograph_Select"), for: .selected)
btn.addTarget(self, action: #selector(self.takePhotoClick), for: .touchUpInside)
v.addSubview(btn)
btn.snp.makeConstraints { (make) -> Void in
make.width.equalTo(50)
make.height.equalTo(50)
make.center.equalTo(v)
}
let btnRT = UIButton()
btnRT.setTitle("返回", for: .normal)
btnRT.setTitleColor(#colorLiteral(red: 0.7450980544, green: 0.1568627506, blue: 0.07450980693, alpha: 1), for: .normal)
btnRT.addTarget(self, action: #selector(self.rtBtn), for: .touchUpInside)
view.addSubview(btnRT)
btnRT.snp.makeConstraints { (make) -> Void in
make.width.equalTo(50)
make.height.equalTo(50)
make.top.equalTo(20)
make.left.equalTo(10)
}
hdbtn.setImage(UIImage(named:"切换"), for: .normal)
hdbtn.setImage(UIImage(named:"切换"), for: .selected)
hdbtn.addTarget(self, action: #selector(self.changehdbtn), for: .touchUpInside)
v.addSubview(hdbtn)
hdbtn.snp.makeConstraints { (make) -> Void in
make.width.equalTo(61)
make.height.equalTo(61)
make.left.equalTo(30)
make.bottom.equalTo(0)
}
focusView.frame = CGRect(x:0,y:0,width:80,height:80)
focusView.layer.borderWidth = 1.0
focusView.layer.cornerRadius = 40
focusView.layer.borderColor = UIColor.green.cgColor
focusView.backgroundColor = UIColor.clear
self.view.addSubview(focusView)
focusView.isHidden = true
let tap = UITapGestureRecognizer.init(target: self, action: #selector(CustomCameraViewController.tap(focusTap:)))
view.addGestureRecognizer(tap)
}
func rtBtn(){
self.dismiss(animated: true, completion: nil)
}
func changehdbtn(){
hdbtn.isSelected = !hdbtn.isSelected
if hdbtn.isSelected {
Preset = 0
captureSession.sessionPreset = AVCaptureSessionPresetPhoto
}else{
Preset = 1
captureSession.sessionPreset = AVCaptureSessionPresetHigh
}
}
func tap(focusTap:UITapGestureRecognizer){
let point = focusTap.location(in: focusTap.view)
focusAtPoin(point: point)
}
//通过手势添加聚焦触摸显示框
func focusAtPoin(point:CGPoint){
focusView.center = point
focusView.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
focusView.alpha = 1
focusView.isHidden = false
UIView.animate(withDuration: 1, animations: { () -> Void in
self.focusView.transform = CGAffineTransform(scaleX: 1, y: 1)
}) { (finish) -> Void in
self.focusView.alpha = 1
self.focusView.isHidden = true
}
}
func takePhotoClick(){
saveToCamera()
}
func beginSession() {
do {
try captureSession.addInput(AVCaptureDeviceInput(device: captureDevice))
stillImageOutput.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]
if captureSession.canAddOutput(stillImageOutput) {
captureSession.addOutput(stillImageOutput)
}
}
catch {
print("error: \(error.localizedDescription)")
}
guard let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) else {
print("no preview layer(没有预览层)")
return
}
self.view.layer.addSublayer(previewLayer)
previewLayer.frame = self.view.layer.frame
captureSession.startRunning()
}
func saveToCamera() {
if let videoConnection = stillImageOutput.connection(withMediaType: AVMediaTypeVideo) {
stillImageOutput.captureStillImageAsynchronously(from: videoConnection, completionHandler: { (CMSampleBuffer, Error) in
if let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(CMSampleBuffer) {
if let cameraImage = UIImage(data: imageData) {
self.image = cameraImage
UIImageWriteToSavedPhotosAlbum(cameraImage, nil, nil, nil)//保存到相册里面
if self.Preset == 1 {
self.gtimage!!(self.image)
}else{
self.gtimage!!(self.image)
}
self.dismiss(animated: true, completion: nil)
}
}
})
}
}
}