做项目的时候,设计要求相机相框中间的景物(贴图)
分析: 一、 用ImagePickerViewController 要达到图片效果 系统底部的工具栏不好控制 放弃
二 、用AVCaptureSession 需要解决的问题有两个
1.中间镂空view
2.裁剪图片得到不被 覆盖的图片
上代码
1.用UIBezierPath画一个矩形且镂空
let path =UIBezierPath(roundedRect:CGRect(x:0, y:0, width:UIDevice.width(), height:UIDevice.hight()), cornerRadius:0)
path.append(UIBezierPath(roundedRect:rect, cornerRadius:0).reversing())
letshapeLayer =CAShapeLayer()
shapeLayer.path= path.cgPath
backView.layer.mask= shapeLayer
2.牌照出来的图片得到图片的高宽都是图片像素宽高,所以用比例去算镂空区域占图片的rect
func clipWithImageRect( image:UIImage?) ->UIImage? {
guardletbgImage = image else{
returnnil
}
//比例
letx = bgImage.size.width/UIDevice.width() *30
lety = bgImage.size.height/UIDevice.hight() *100
letw = (UIDevice.width() -30*2)/UIDevice.width() * bgImage.size.width
leth = (UIDevice.hight() -100*3)/UIDevice.hight() * bgImage.size.height
let clipFrame =CGRect(x: x, y: y, width: w, height: h)
letrect_Scale =CGRect(x: clipFrame.origin.x, y: clipFrame.origin.y, width: clipFrame.size.width, height: clipFrame.size.height)
letcgImageCorpped = bgImage.cgImage?.cropping(to: rect_Scale)
varimg_Clip =UIImage.init(cgImage: cgImageCorpped!, scale:1, orientation:UIImage.Orientation.up)
img_Clip =UIImage(cgImage: (img_Clip.cgImage)!, scale: img_Clip.scale, orientation: .left)
returnimg_Clip
}
效果图:
资源 : Demo