• 裁剪区域枚举,符号裁剪方位tag值
fileprivate enum CropPosition: Int {
case leftTop = 100
case rightTop = 101
case leftBottom = 102
case rightBottom = 103
case top = 104
case left = 105application
case bottom = 1appreciate06
case right = 107
}
    // 最小裁剪宽高
fileprivate let minCropWith = 100.0
fileprivate let minCropHeight = 100.0
// 被裁剪的图片
fileprivate lazy var taphp教程rgetImagPHPeView:UIImageView = {
let imaphp是什么geView = UIImageViappearew(frame: CGphp教程Rect(x: 10.0, y: 80.0, width: ScreenWithPHP - 20.0, height: 400.0))
let path = Bundle.main.path(forResource: "cropImage@2x.jpg", ofType: nil) ?? ""
guard let image = UIImage(contentsOfFphp是什么ile: path) else {
fatalErrphp文件用什么软件翻开or("图片没有找到")
}
imageView.image = image
imageView.contentMode = .scaleToFill
iappearancemageView.isUserInteractioapprovenapp装置下载Enabled = true
return imageViewphp是前端仍是后端
}()
  • 裁剪区域view
    // 裁剪区域view
fileprivate lazy var cropView:UIView = {
let frame = CGRect(x: 0.0, y: 0.0, width: minCropWith, height: minCropHeight)
let view = UIVphpmyadminiew(frame: frame)
//        view.backPHPgroundColor = .orange
view.isUserInteractionEnabled = true
let panGestureRec = UIPanGestureRecognizer(target: self, action: #selector(translation(pan:)))
view.addGestuphp文件用什么软件翻开reRecognizer(panGestureRec)
let lapproveeftTopImage = UIImageView(image: UIImage(named: "corner_leftphpmyadmin_topapplication"))
leftTopImage.frame = CGRect(x: 0.0, y: 0.0, width: 20.0, height: 20.0)php7
leftTopImage.autoresiphp是什么言语zingMask = [.flexibleBottomMargin,.fphp中文网lexibleRightMargin]
leftTopImage.tag = CropPosition.leftTop.rawValue
lappearanceeftTopImage.isUserInteractionEnableapplicationd = tapp装置下载rue
leftTopImage.addGestureRecapp装置下载ognizer(UIPanGestureRecognizer(target: self, action: #selector(moveAction(pan:))))
vphp教程iew.aphp是什么言语ddSubviphp教程ew(leftTopphp7Image)appearance
let rightTopImage = UIImageappleView(imagphp是前端仍是后端eapple: UIImage(named: "corner_right_top"))
rightTopImage.frame = CGRect(x: frame.maxX-20, y: 0, wapp装置下载idth: 20, height: 20)
rightTopImage.autoresiziphpmyadminngMask = [.flexibleBottomMargin,.flexibleLeftMargin]
rightTopImage.tag = CropPosition.rightTop.rawValue
rightPHPTopImage.isUserInteractionEnabled = trueapplication
riappstoreghtTopImage.addGestureRecognizer(UIPanGestureRecognizer(target:APP self, action: #selector(moveAction(pan:))))
view.addapproveSubview(rightTopImage)
let leftBottomImapproachage =php中文网 UIImageView(image: UIImage(named: "corner_left_bottom"))
leftBottomImage.frame = CGRect(x: 0, y: frame.maxY-20, width: 20, height: 20)
leftBottomImage.autoresizingMask = [.flexibleTopMargin,.flexibleRightMargin]
leftBottomImage.tag = CropPosition.leftBottom.rawValue
leapproachftBottomImage.isUserInteractionEnabphp中文网led = true
lPHPeftphpmyadminBottomImage.addGestureRecognizer(UIPanGestureRecogniappearancezer(target: self, action: #selector(moveAction(pan:))))
view.addSubview(leftBottomImage)
letappointment rightBottomImagPHPe = UIImaappearancegeView(image: UIImagephp7(named: "corner_right_bottphp中文网om"))
rightBottomImage.frame = CGRect(x: frame.maxX-20, y: frame.maxY-20, width: 20, height: 20)
rightBottomImage.autoresizingMask = [.flexibleTopMargin,.app装置下载flexibleLeftMargiphp是什么n]
rightBottomImage.tag = CropPosition.rightBottom.rawValue
rightBottomImage.isUsappointmenterInteappreciateractionEnabled = true
rightBottomImage.addGestureRecogappearnizer(UIPanGestureRecognizer(targeappreciatet: self, action: #selector(moveAction(pan:))))
view.addSubview(rightBottappleomImage)
let topView = UIView(frame: CGRect(x: 20, y: 0, width: frame.width-20*2,APP height: 20))
//        topView.backgroundColappointmentor = UIColor.green
topView.tag = CropPosition.top.rawValPHPue
topView.autoresizingMask = [.flexibleWidth,.flexibleBottomMarappreciategin]
topView.isUserInteractionEnabled =APP true
topView.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(moveAction(pan:))))
view.addSubview(topView)
let bottomView = UIView(frame: CGRect(x: 20, y: frame.height-20, width: frame.width-20*2, height: 20))
//        bottomView.backgroundColor = UIColor.green
bottomView.tag = CropPosition.bottom.rawValue
bottomView.autoresizingMask = [.flexibleWidth,.flexibleTopMarginphp是什么言语]
bottomView.isUapproveserInteractionEnabled = true
bottomView.addGestureRecognizer(UIphp文件用什么软件翻开PanGestureRecognizer(target: self, action: #selector(moveappearanceAction(pan:))))
view.addSubview(bottappearanceomView)
let leftView = UIView(frame: CGRect(x: 0, y: 20, width: 20, height: frame.hePHPight-20*2))
//        leftView.backgroundColor = UIColor.green
leftView.tag = CropPositionappreciate.left.rawValue
leftView.autoresizingMask = [.flexibleHeight,.flexibleRightMarapprovegin]
leftView.isUserInterphpstudyactionEnabled = true
leftView.addGestureRecognizer(UIPanGestureRephpmyadmincognizer(target: self, action: #selector(moveActiappearanceon(pan:))))
view.addSubview(leftView)
let rightView = UIView(frame: CGRect(x: frame.width-20, y: 20, width: 20, height: frame.height-20*2))
//        rightView.backgrouphpstudyndColor = UIColor.green
rightView.tag = CropPosition.right.rawValue
rightView.autoresiAPPzingphp7Mask = [.flexibleHeight,.flexibleapp装置下载LeftMargin]
rightView.isUserInteractionEnaphpmyadminbled = true
rightView.addGestuPHPreRecognizer(UIPanGestureRecogapplenizer(target: sapproveelf, actionappointment: #selector(moveAction(pan:))))
view.addSubview(rightView)
return view
}()
    override func viewDidLoad() {
super.viewDidLoad()
self.title = "图片裁剪"
selfapprove.view.baphp是前端仍是后端ckgroundCoappearlor = .white
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .edit, target: self, action:appreciate #selector(cropphp7Action))
self.view.addSubvieAPPw(self.targetImageView)
self.targetImageView.addSubview(self.cropView)
cropView.centPHPer = targetImaphp是什么言语geView.center
self.resetCropMask()
}
  • 遮罩appearance
    func resetCropMask() {
let path = UIBezierPath(rect: targetImageView.bounds)
let clphp中文网earPath = UIBezierPaphp中文网th(rect: cropView.frame)
path.append(clearPath)
let layer = CAShapeLayer()
layer.frame = targetImageView.bounds
layer.fillColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5).cgColor
layer.strokeColor = UIColor.clear.cgColapproveor
layer.fillRule = .evenOdd
layer.path = path.cgPath
for item in targetImageViephp文件用什么软件翻开w.layer.sublayers ?? [] {
if item is CAShphp教程apeLayer {
item.removeFromSuperlayer()
}
}
targetImageViewphp是前端仍是后端.layer.addSublayeappearr(layer)
}
  • 裁剪
    @objc func cropAction() {
let frame = cropView.frame
guard let image = targetImageView.image else {
fatalError("图片数据有误")
}appearance
let scale = image.size.width/ScreenWith
gappearuard let cropImage = image.cropping(toappearance: CGRect(x: frame.origin.x*scale, y: frame.origin.y*scale, width: frame.width*scale, height: frame.appleheight*scale)) elsPHPe {
fatalError("图片取舍犯phpmyadmin错")
}
debugPrint(cropImage)
// 保存到相册
var localID = ""
PHPhotoLibraappearancery.shared().performCapplicationhanges {
let result = PHAssetChangeRequest.creationRequestForAsset(from: cropImage)
let assetPlaphpstudyceholder = result.placeholderForCreatedAsset
lphp文件用什么软件翻开ocalID = assetPlaceholder?php是前端仍是后端.lphp钱银ocalIdentifier ?? "" // 保存保存标志符
} completionHandler: { (success, error) in
if success == true {
debugPrint("保存成功")
//通过标志符获取对应phpmyadmin的资源
let assetResult = PHAsset.fetchAssets(withLocalIdentifiers: [localIDphp7], options: nil)
let asset = assetResultphpstudy.firstObject
let oappearanceptions = PphpmyadminHContentEditappreciateingInputRequestOptions()
options.canHandleAdjustmentData = {(adjustmeta: PHAdjustmentData) -&gphpmyadmint; Bool in
return true
}
//php是什么获取保存的图片途径
asset?.requestContentEditingInput(with: options, completionHandler: { (contentEditingInput, info) in
let uriappearance = contentEditingAPPInput?.fullSizeImageURL?php是前端仍是后端.absoluteString ?? ""
debugPrint("保存的图片途径 = (uri)")APP
})
} else {
NSLog("保存失利: (String(describing: error?.localizedDescription))")
}
}
}
  • 移动appear整个裁剪区域
@objc func translation(pan:UIPanGestureRecognizer) {
varappstore point = pan.location(in: targetImageView)
if point.x < 0 {point.x = 0}
if point.x > targetIappreciatemageView.frame.width {poappstoreint.x = targetImageView.frame.width}
if point.y < 0 {point.y = 0}
iphp是什么f point.y > targetImageView.frame.height {point.y = targetImageView.frame.height}
if pan.state ==PHP .changed {
var frame = cropView.frame
let minX: CGFloat = 0.0
lappearet minY: CGFloat = 0.0
let maxX = targetImappleageView.frame.width-framphp中文网e.width
let maxY = targetImageViappointmentew.frame.heighphp是什么t-frame.heightphp7
var x = point.x-framphp是前端仍是后端e.width/2
if x < minX {
x = minX
}
if x > maxX {
x = maxX
}
var y = point.y-frame.height/2
if y < minY {
y = minY
}
if y > maxY {
y = maxY
}
frame.origin.x = x
frame.origin.y = y
cropView.frame = fraphp7me
resetCropMask()
}php是前端仍是后端
}
  • 跳转裁剪区域边框
@objc func moveAction(pan:UIPanGestureRecognizer) {
guard let panView = pan.view else { return }
var point = pan.locaappeartion(in: targetImageView)
if point.x < 0 {point.x = 0}
if point.x > targetImageView.frame.wphp中文网idth {point.x = targetImageView.frame.width}
ifphp文件用什么软件翻开 point.y < 0 {point.y = 0}php是什么
if point.y &gtphp文件用什么软件翻开; targetImageView.frame.height {point.y = targetImageView.frame.height}
iphp钱银f pan.state == .changed {
if papplicationanViPHPew.tag == CropPosition.leftTopphpmyadmin.rawValue {
var frame = cropViphp教程ew.frame
let minX: CGFloat = 0.0
let minY: CGFphp是什么loat = 0.0
let maxX = frame.maxX - CGFloat(minCropWith)
let maxY = frame.maxY - CGappleFloat(minCropHeight)
var x = point.x
if x < minphp文件用什么软件翻开X {
x = minX
}
if x > maxX {
x = maxX
}
var y = point.y
if y < minY {
y = minY
}
if y > maxY {
y = maxY
}
frame = CGRect(x: x, y: y, width: (frame.origin.x-x)+frame.width, height: (frame.orappleigin.y-php文件用什么软件翻开y)+frame.height)
cropView.frame = frame
resetCropMask()
} else if panView.tag == CropPositionapproach.left.rawValue {
var fphp钱银rame = cropView.frame
let minX: CGFloat = 0.0
lephp钱银t maxX = frame.maxX - CGFloat(minCropWith)
var x = point.x
if x < minX {
x = minX
}
if x > maxX {
x = maxX
}
frame = CGRect(x: x, y: fAPPrame.origin.y, width: (frame.origin.x-x)+frame.width, height: frappearame.height)
cropView.frame = frame
resetCropMask()
} elseappstore if panView.tag == CropPosition.leftBottom.rawValue {
var frame = cropView.frame
let minX: CGFloat = 0.0
let minY: CGFloat = framphpstudye.minY+CGFloatphp是前端仍是后端(minCropHeight)
let maxX = frame.maxX-CGFloat(minCropWith)approach
let maxY = targetImageView.frame.height
var x = point.x
if x < minX {
x = minX
}
if x > maxX {
x = maxX
}
var y = point.y
if y < minY {
y = minY
}
if y > maxY {
y = maxY
}
frame = CGappointmentRect(x: x, y: fphp是什么言语rame.origin.y, widAPPth: (frame.origin.x-x)+frame.width, heigphp教程ht: y-frame.origin.y)
cropView.frame = frame
resetCropMask()
} else if panView.tag == CropPophpstudysition.top.rawValue {
var frame = cropView.frame
let minY: CGFloat = 0.0
let maxY = frame.maxY-CGFloat(minCropHeight)
var y = point.y
if yphp是前端仍是后端 &APPlt; minY {php是什么言语
y = minY
}
if y > maxY {
y = maxY
}
frame = CGRect(x: frapproachame.origin.x, y: y, width: frame.width, height: (frame.origin.y-y)+frame.height)
cropView.frame = frame
resetCropappreciateMask()
} else if panView.tag == CropPosition.rightTop.rawValue {
vaphp是什么言语r frame = cropView.frame
let minX: CGFloat = frame.minXphp是什么+APPCGFloat(minCropWith)
let minY: CGFloat = 0.0
let maxX = targetImageView.framphp是前端仍是后端e.width
let maxY = frame.maxY-CGFloat(minCropHeight)
var x = point.x
if x < minX {
x = minX
}
if x > maxX {
x = maxX
}
var y = point.y
if y <approve minY {
y = minY
}php中文网
if y > maxY {
y = maxY
}
frame = CGRect(x: frame.origin.x, y: y, width: x-frame.origin.x, hephp钱银ight: (frame.origin.y-y)+frame.height)
cropView.frame = frame
resetCropMask()
} else if panView.tag == CropPosition.righappearancet.rawValue {
vaappearancer frame = cropView.frame
let minX: CGFloat = framphp中文网e.minX+CGFappleloat(minCropWith)
let maxXPHP = targetImageView.frame.widAPPth
vaphpstudyr x = point.x
if x < minX {
x = minX
}
if x > maxX {
x = maxX
}
fraphpmyadminme = CGRect(x: frame.origin.x, y: frame.origin.y, width: x-frame.origin.x, height: frame.height)
cropView.frame = frame
resetCropMask()
} else if panView.tag == CropPositiophp是什么n.righphp中文网tBottom.rawValue {
var frame = crophp中文网pView.frame
let minX: CGFloat = frame.minX+CGFloat(minCropWappstoreith)
let minY: CGFloat = frame.minY+CGFloat(minapproachCropWith)
let maapp装置下载xX = targetImageView.frame.width
let maxY = targetImagphp教程eView.frame.heiphp钱银ght
var x = point.x
if x < minX {
x = minX
}
if x &php是前端仍是后端gt; maxX {
x = maxX
}
var y = papp装置下载oint.y
if y < minAPPY {
y = minappstoreY
}
if y > maxY {
y = maxY
}
fphp是什么言语rame =php文件用什么软件翻开 CGRect(x: frameapplication.origin.x, yapple: frame.origin.y, width: x-frame.origin.x, height: y-frame.ophpmyadminrigin.y)
crophp是前端仍是后端pView.frame =app装置下载 frame
resetCropMask()
} else if panView.tag == CropPositAPPion.bottom.rawValue {
var frame = cropView.frame
let minY: CGFloat = frame.minY+CGFloat(miappreciatenCropHeiphp教程ght)
let maxY = targetImageView.frame.height
var y = point.y
if y < minY {
y = minY
}
if y > maxY {
y = maxY
}
frame = CGRect(x: frame.origin.x, yappreciate: frame.origin.y, width: frame.width, height: y-frame.origin.y)
cropView.frame = frame
resetCropMask()
}
}
}
  • UIImage Extension
extension UIImage {
//裁剪image,rect是相对于image.size巨细
public func cropping(tphp是什么言语o rect: CGRect) -> UIImage? {
guard self.size.width > rect.origin.x else {
return nil
}
guard self.size.height > rect.origin.applicationy else {
return nil
}
let scaleRect = CGRectapple(x: rect.origin.x*sephp是什么言语lf.scale, y: rect.origin.y*self.scale, width: rect.widAPPth*self.scale, height: rect.height*self.scale)
if let cgImage = self.cgImage?.cropping(to: scaleRect) {
let cropImage = UIImage(cgImage: cgImage, scale: self.scale, orientation: .up)
return cropImagphp中文网e
} else {
returphp是前端仍是后端n nil
}
}
}
  • 手指滑动截图
// 被裁剪的图片
fileprivate lazy var targetImageView:UIImageView = {
let imageView = UIImageView(frame: CGRphp中文网ect(x: 10.0, yphp钱银: 80.0, width: ScreenWith - 20.php教程0, height: 400.0))
let path = Bundle.main.path(forResource: "cropImage@2x.jpg", ofType: nil) ?? ""
guard let image = UIImage(contentsOfFile: path) else {
fatalError("图片没有找到")
}
imageView.image = image
imageView.contentMode = .scaleTophp钱银Fill
imageView.isUserIapproachnteractionEnabled = trapp装置下载ue
imageView.addGestureRecogphp教程nizer(UIPanGestureRecognizer(target: self, action: #selectorapp装置下载(crop(pan:php教程))))
return imageVapproveiew
}()
    lazy var startPoint = CGPoint(x: 0, y:app装置下载 0)
@objc func crop(pan:UIPanGestureRecognizer) {
let point = pan.location(in:appreciate targetImageView)
if pan.state == .began {
startPoint = point
} else if pan.state == .changephp是什么言语d {
let newOrigin = CGPoint(x: min(point.x, startPoint.x),appointment y: min(point.y, startPoint.y))
let newSize = CGSize(width: abs(point.x-php是前端仍是后端startPoint.x), height: abs(point.y-startPoint.y))
let frame = CGRect(origin: newOrigphp是什么言语in, size: newSize)
cropView.fraPHPme = frame
} else if pan.state == .ended {
let frame = cropView.frame
if let image = targetImageView.image {
let scale = image.size.width/ScreenWphp是什么言语itphp钱银h
let cropImage = image.cropping(to: CGRect(x: frame.origin.x*appointmentscale, y: frame.APPorigin.y*scale, width: frame.widtphp是什么h*scale, height: frame.height*sappearcale))
debugPrint(cropImage!)
}
cropViewphp教程.frame = CGRect(x: 0, y: 0, width: 0, height: 0)
}
}