Skip to content

Commit af09d1f

Browse files
author
Chris Hu
committed
DemoImagePreview: Crop采用MyZoomScrollView
1 parent a8048ad commit af09d1f

File tree

1 file changed

+70
-45
lines changed

1 file changed

+70
-45
lines changed

DemoImagePreview/DemoImagePreview/CropViewController.swift

Lines changed: 70 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,96 +8,120 @@
88

99
import UIKit
1010

11-
class CropViewController: UIViewController {
1211

13-
var imageView: UIImageView!
14-
var maskView: UIView!
15-
var ratioView: UIView!
12+
private let offset: CGFloat = 10.0
13+
14+
class CropViewController: UIViewController {
15+
16+
var topBar: UIView!
17+
var btnRight: UIButton!
1618

17-
var imageOriginal: UIImage! // 原图
18-
var imageCropped: UIImage! // 裁剪结果图
19+
var myZoomScrollView: MyZoomScrollView! // 原图的scrollView
20+
var cropMaskView: UIView! // 裁剪的maskView
21+
var ratioView: UIView! // 裁剪框
1922

23+
var imageOriginal: UIImage! // 原图
24+
var imageCropped: UIImage! // 裁剪结果图
2025

26+
}
27+
28+
// MARK: - VC生命周期
29+
extension CropViewController {
2130
override func viewDidLoad() {
2231
super.viewDidLoad()
2332

24-
view.backgroundColor = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)
25-
33+
view.backgroundColor = UIColor.black
34+
2635
imageOriginal = UIImage(named: "Model.png")
2736

28-
initImageView()
37+
initTopBar()
38+
39+
initMyZoomScrollView()
2940

3041
initCropMaskView()
3142

3243
initRatioView()
3344

3445
maskClipping()
46+
47+
}
48+
49+
override var prefersStatusBarHidden: Bool {
50+
return true
3551
}
52+
}
3653

54+
// MARK: - topBar
55+
extension CropViewController {
56+
func initTopBar() {
57+
topBar = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 40))
58+
topBar.backgroundColor = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)
59+
view.addSubview(topBar)
60+
61+
btnRight = UIButton(frame: CGRect(x: topBar.frame.width - 70, y: 0, width: 70, height: topBar.frame.height))
62+
btnRight.setTitle("裁剪", for: .normal)
63+
btnRight.addTarget(self, action: #selector(CropViewController.actionBtnRight), for: .touchUpInside)
64+
topBar.addSubview(btnRight)
65+
}
66+
67+
func actionBtnRight() {
68+
// TODO 裁剪操作
69+
let image = cropImage()
70+
print(image?.size)
71+
}
72+
3773
}
3874

39-
// MARK: - 裁剪操作
40-
let height = CGFloat(300)
4175
extension CropViewController {
4276

43-
func initImageView() {
44-
imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: height))
45-
imageView.center = view.center
46-
imageView.image = imageOriginal
47-
view.insertSubview(imageView, at: 0)
48-
imageView.isUserInteractionEnabled = true
77+
func initMyZoomScrollView() {
78+
let height = view.frame.height - topBar.frame.height - 250 - offset * 3
79+
80+
myZoomScrollView = MyZoomScrollView(frame: CGRect(x: 0, y: topBar.frame.height + offset, width: view.frame.width, height: height))
81+
view.addSubview(myZoomScrollView)
82+
myZoomScrollView.backgroundColor = UIColor.red
83+
myZoomScrollView.image = imageOriginal
4984
}
5085

5186
func initCropMaskView() {
52-
maskView = UIView(frame: view.bounds)
53-
view.addSubview(maskView)
54-
maskView.backgroundColor = UIColor.black
55-
maskView.alpha = 0.2
56-
maskView.center = view.center
57-
maskView.isUserInteractionEnabled = false
87+
cropMaskView = UIView(frame: view.bounds)
88+
cropMaskView.backgroundColor = UIColor.green
89+
cropMaskView.alpha = 0.2
90+
view.addSubview(cropMaskView)
91+
cropMaskView.isUserInteractionEnabled = false
5892
}
5993

6094
func initRatioView() {
61-
let height = 200
62-
let width = height / 16 * 9
63-
ratioView = UIView(frame: CGRect(x: 0, y: 0, width: width, height: height))
95+
let height = myZoomScrollView.frame.height + 4
96+
let width = CGFloat(height / 16 * 9)
97+
ratioView = UIView(frame: CGRect(x: (view.frame.width - width) / 2, y: topBar.frame.height + offset - 2, width: width, height: height))
6498
ratioView.layer.borderColor = UIColor.yellow.cgColor
6599
ratioView.layer.borderWidth = 1
66-
ratioView.center = imageView.center
100+
ratioView.center = myZoomScrollView.center
67101
view.addSubview(ratioView)
68102
ratioView.isUserInteractionEnabled = false
69103
}
70104

71105
func maskClipping() {
72106
let maskLayer = CAShapeLayer()
73107
let path = CGMutablePath()
74-
let left = CGRect(x: 0, y: 0, width: ratioView.frame.minX, height: maskView.frame.height)
75-
let right = CGRect(x: ratioView.frame.maxX, y: 0, width: maskView.frame.width - ratioView.frame.maxX, height: maskView.frame.height)
76-
let top = CGRect(x: 0, y: 0, width: maskView.frame.width, height: ratioView.frame.minY)
77-
let bottom = CGRect(x: 0, y: ratioView.frame.maxY, width: maskView.frame.size.width, height: maskView.frame.height - ratioView.frame.maxY)
108+
let left = CGRect(x: 0, y: 0, width: ratioView.frame.minX, height: cropMaskView.frame.height)
109+
let right = CGRect(x: ratioView.frame.maxX, y: 0, width: cropMaskView.frame.width - ratioView.frame.maxX, height: cropMaskView.frame.height)
110+
let top = CGRect(x: 0, y: 0, width: cropMaskView.frame.width, height: ratioView.frame.minY)
111+
let bottom = CGRect(x: 0, y: ratioView.frame.maxY, width: cropMaskView.frame.size.width, height: cropMaskView.frame.height - ratioView.frame.maxY)
78112
path.addRects([left, right, top, bottom])
79113
maskLayer.path = path
80-
maskView.layer.mask = maskLayer;
114+
cropMaskView.layer.mask = maskLayer
81115
}
82116

83117
}
84118

85119
extension CropViewController {
86-
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
87-
super.touchesBegan(touches, with: event)
88-
89-
guard let touch = touches.first else { return }
90-
let touchPoint = touch.location(in: view)
91-
imageView.center = touchPoint
92-
}
93120

94-
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
95-
super.touchesMoved(touches, with: event)
96-
97-
guard let touch = touches.first else { return }
98-
let touchPoint = touch.location(in: view)
99-
imageView.center = touchPoint
121+
func cropImage() -> UIImage? {
122+
return imageOriginal
100123
}
124+
101125
}
102126

103127
extension UIImage {
@@ -112,3 +136,4 @@ extension UIImage {
112136
return imageCropped
113137
}
114138
}
139+

0 commit comments

Comments
 (0)