做項目的時候涉枫,設(shè)計要求相機相框中間的景物(貼圖)
分析: 一邢滑、 用ImagePickerViewController 要達到圖片效果 系統(tǒng)底部的工具欄不好控制 放棄?
? ? ? ? ?二 、用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.牌照出來的圖片得到圖片的高寬都是圖片像素寬高愿汰,所以用比例去算鏤空區(qū)域占圖片的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