Skip to content

Commit c671631

Browse files
committed
Fix #21
1 parent 7796112 commit c671631

File tree

3 files changed

+96
-86
lines changed

3 files changed

+96
-86
lines changed

Example/Example/ViewController.swift

Lines changed: 76 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import UIKit
99
import ZLImageEditor
1010

1111
class ViewController: UIViewController {
12-
1312
var editImageToolView: UIView!
1413

1514
var editImageDrawToolSwitch: UISwitch!
@@ -44,8 +43,8 @@ class ViewController: UIViewController {
4443
}
4544

4645
func setupUI() {
47-
self.title = "Main"
48-
self.view.backgroundColor = .white
46+
title = "Main"
47+
view.backgroundColor = .white
4948

5049
func createLabel(_ title: String) -> UILabel {
5150
let label = UILabel()
@@ -57,154 +56,154 @@ class ViewController: UIViewController {
5756

5857
let spacing: CGFloat = 20
5958
// Container
60-
self.editImageToolView = UIView()
61-
self.view.addSubview(self.editImageToolView)
62-
self.editImageToolView.snp.makeConstraints { (make) in
59+
editImageToolView = UIView()
60+
view.addSubview(editImageToolView)
61+
editImageToolView.snp.makeConstraints { make in
6362
make.top.equalTo(self.view.snp.topMargin).offset(5)
6463
make.left.equalTo(self.view).offset(20)
6564
make.right.equalTo(self.view).offset(-20)
6665
}
6766

6867
let drawToolLabel = createLabel("Draw")
69-
self.editImageToolView.addSubview(drawToolLabel)
70-
drawToolLabel.snp.makeConstraints { (make) in
68+
editImageToolView.addSubview(drawToolLabel)
69+
drawToolLabel.snp.makeConstraints { make in
7170
make.top.equalTo(self.editImageToolView).offset(spacing)
7271
make.left.equalTo(self.editImageToolView)
7372
}
7473

75-
self.editImageDrawToolSwitch = UISwitch()
76-
self.editImageDrawToolSwitch.isOn = config.tools.contains(.draw)
77-
self.editImageDrawToolSwitch.addTarget(self, action: #selector(drawToolChanged), for: .valueChanged)
78-
self.editImageToolView.addSubview(self.editImageDrawToolSwitch)
79-
self.editImageDrawToolSwitch.snp.makeConstraints { (make) in
74+
editImageDrawToolSwitch = UISwitch()
75+
editImageDrawToolSwitch.isOn = config.tools.contains(.draw)
76+
editImageDrawToolSwitch.addTarget(self, action: #selector(drawToolChanged), for: .valueChanged)
77+
editImageToolView.addSubview(editImageDrawToolSwitch)
78+
editImageDrawToolSwitch.snp.makeConstraints { make in
8079
make.left.equalTo(drawToolLabel.snp.right).offset(spacing)
8180
make.centerY.equalTo(drawToolLabel)
8281
}
8382

8483
let cropToolLabel = createLabel("Crop")
85-
self.editImageToolView.addSubview(cropToolLabel)
86-
cropToolLabel.snp.makeConstraints { (make) in
84+
editImageToolView.addSubview(cropToolLabel)
85+
cropToolLabel.snp.makeConstraints { make in
8786
make.centerY.equalTo(drawToolLabel)
8887
make.left.equalTo(self.editImageToolView.snp.centerX)
8988
}
9089

91-
self.editImageClipToolSwitch = UISwitch()
92-
self.editImageClipToolSwitch.isOn = config.tools.contains(.clip)
93-
self.editImageClipToolSwitch.addTarget(self, action: #selector(clipToolChanged), for: .valueChanged)
94-
self.editImageToolView.addSubview(self.editImageClipToolSwitch)
95-
self.editImageClipToolSwitch.snp.makeConstraints { (make) in
90+
editImageClipToolSwitch = UISwitch()
91+
editImageClipToolSwitch.isOn = config.tools.contains(.clip)
92+
editImageClipToolSwitch.addTarget(self, action: #selector(clipToolChanged), for: .valueChanged)
93+
editImageToolView.addSubview(editImageClipToolSwitch)
94+
editImageClipToolSwitch.snp.makeConstraints { make in
9695
make.left.equalTo(cropToolLabel.snp.right).offset(spacing)
9796
make.centerY.equalTo(cropToolLabel)
9897
}
9998

10099
let imageStickerToolLabel = createLabel("Image sticker")
101-
self.editImageToolView.addSubview(imageStickerToolLabel)
102-
imageStickerToolLabel.snp.makeConstraints { (make) in
100+
editImageToolView.addSubview(imageStickerToolLabel)
101+
imageStickerToolLabel.snp.makeConstraints { make in
103102
make.top.equalTo(drawToolLabel.snp.bottom).offset(spacing)
104103
make.left.equalTo(self.editImageToolView)
105104
}
106105

107-
self.editImageImageStickerToolSwitch = UISwitch()
108-
self.editImageImageStickerToolSwitch.isOn = config.tools.contains(.imageSticker)
109-
self.editImageImageStickerToolSwitch.addTarget(self, action: #selector(imageStickerToolChanged), for: .valueChanged)
110-
self.editImageToolView.addSubview(self.editImageImageStickerToolSwitch)
111-
self.editImageImageStickerToolSwitch.snp.makeConstraints { (make) in
106+
editImageImageStickerToolSwitch = UISwitch()
107+
editImageImageStickerToolSwitch.isOn = config.tools.contains(.imageSticker)
108+
editImageImageStickerToolSwitch.addTarget(self, action: #selector(imageStickerToolChanged), for: .valueChanged)
109+
editImageToolView.addSubview(editImageImageStickerToolSwitch)
110+
editImageImageStickerToolSwitch.snp.makeConstraints { make in
112111
make.left.equalTo(imageStickerToolLabel.snp.right).offset(spacing)
113112
make.centerY.equalTo(imageStickerToolLabel)
114113
}
115114

116115
let textStickerToolLabel = createLabel("Text sticker")
117-
self.editImageToolView.addSubview(textStickerToolLabel)
118-
textStickerToolLabel.snp.makeConstraints { (make) in
116+
editImageToolView.addSubview(textStickerToolLabel)
117+
textStickerToolLabel.snp.makeConstraints { make in
119118
make.centerY.equalTo(imageStickerToolLabel)
120119
make.left.equalTo(self.editImageToolView.snp.centerX)
121120
}
122121

123-
self.editImageTextStickerToolSwitch = UISwitch()
124-
self.editImageTextStickerToolSwitch.isOn = config.tools.contains(.textSticker)
125-
self.editImageTextStickerToolSwitch.addTarget(self, action: #selector(textStickerToolChanged), for: .valueChanged)
126-
self.editImageToolView.addSubview(self.editImageTextStickerToolSwitch)
127-
self.editImageTextStickerToolSwitch.snp.makeConstraints { (make) in
122+
editImageTextStickerToolSwitch = UISwitch()
123+
editImageTextStickerToolSwitch.isOn = config.tools.contains(.textSticker)
124+
editImageTextStickerToolSwitch.addTarget(self, action: #selector(textStickerToolChanged), for: .valueChanged)
125+
editImageToolView.addSubview(editImageTextStickerToolSwitch)
126+
editImageTextStickerToolSwitch.snp.makeConstraints { make in
128127
make.left.equalTo(textStickerToolLabel.snp.right).offset(spacing)
129128
make.centerY.equalTo(textStickerToolLabel)
130129
}
131130

132131
let mosaicToolLabel = createLabel("Mosaic")
133-
self.editImageToolView.addSubview(mosaicToolLabel)
134-
mosaicToolLabel.snp.makeConstraints { (make) in
132+
editImageToolView.addSubview(mosaicToolLabel)
133+
mosaicToolLabel.snp.makeConstraints { make in
135134
make.top.equalTo(imageStickerToolLabel.snp.bottom).offset(spacing)
136135
make.left.equalTo(self.editImageToolView)
137136
}
138137

139-
self.editImageMosaicToolSwitch = UISwitch()
140-
self.editImageMosaicToolSwitch.isOn = config.tools.contains(.mosaic)
141-
self.editImageMosaicToolSwitch.addTarget(self, action: #selector(mosaicToolChanged), for: .valueChanged)
142-
self.editImageToolView.addSubview(self.editImageMosaicToolSwitch)
143-
self.editImageMosaicToolSwitch.snp.makeConstraints { (make) in
138+
editImageMosaicToolSwitch = UISwitch()
139+
editImageMosaicToolSwitch.isOn = config.tools.contains(.mosaic)
140+
editImageMosaicToolSwitch.addTarget(self, action: #selector(mosaicToolChanged), for: .valueChanged)
141+
editImageToolView.addSubview(editImageMosaicToolSwitch)
142+
editImageMosaicToolSwitch.snp.makeConstraints { make in
144143
make.left.equalTo(mosaicToolLabel.snp.right).offset(spacing)
145144
make.centerY.equalTo(mosaicToolLabel)
146145
}
147146

148147
let filterToolLabel = createLabel("Filter")
149-
self.editImageToolView.addSubview(filterToolLabel)
150-
filterToolLabel.snp.makeConstraints { (make) in
148+
editImageToolView.addSubview(filterToolLabel)
149+
filterToolLabel.snp.makeConstraints { make in
151150
make.centerY.equalTo(mosaicToolLabel)
152151
make.left.equalTo(self.editImageToolView.snp.centerX)
153152
}
154153

155-
self.editImageFilterToolSwitch = UISwitch()
156-
self.editImageFilterToolSwitch.isOn = config.tools.contains(.filter)
157-
self.editImageFilterToolSwitch.addTarget(self, action: #selector(filterToolChanged), for: .valueChanged)
158-
self.editImageToolView.addSubview(self.editImageFilterToolSwitch)
159-
self.editImageFilterToolSwitch.snp.makeConstraints { (make) in
154+
editImageFilterToolSwitch = UISwitch()
155+
editImageFilterToolSwitch.isOn = config.tools.contains(.filter)
156+
editImageFilterToolSwitch.addTarget(self, action: #selector(filterToolChanged), for: .valueChanged)
157+
editImageToolView.addSubview(editImageFilterToolSwitch)
158+
editImageFilterToolSwitch.snp.makeConstraints { make in
160159
make.left.equalTo(filterToolLabel.snp.right).offset(spacing)
161160
make.centerY.equalTo(filterToolLabel)
162161
}
163162

164163
let adjustToolLabel = createLabel("Adjust")
165-
self.editImageToolView.addSubview(adjustToolLabel)
166-
adjustToolLabel.snp.makeConstraints { (make) in
164+
editImageToolView.addSubview(adjustToolLabel)
165+
adjustToolLabel.snp.makeConstraints { make in
167166
make.top.equalTo(mosaicToolLabel.snp.bottom).offset(spacing)
168167
make.left.equalTo(self.editImageToolView)
169168
}
170169

171-
self.editImageAdjustToolSwitch = UISwitch()
172-
self.editImageAdjustToolSwitch.isOn = config.tools.contains(.adjust)
173-
self.editImageAdjustToolSwitch.addTarget(self, action: #selector(adjustToolChanged), for: .valueChanged)
174-
self.editImageToolView.addSubview(self.editImageAdjustToolSwitch)
175-
self.editImageAdjustToolSwitch.snp.makeConstraints { (make) in
170+
editImageAdjustToolSwitch = UISwitch()
171+
editImageAdjustToolSwitch.isOn = config.tools.contains(.adjust)
172+
editImageAdjustToolSwitch.addTarget(self, action: #selector(adjustToolChanged), for: .valueChanged)
173+
editImageToolView.addSubview(editImageAdjustToolSwitch)
174+
editImageAdjustToolSwitch.snp.makeConstraints { make in
176175
make.left.equalTo(adjustToolLabel.snp.right).offset(spacing)
177176
make.centerY.equalTo(adjustToolLabel)
178177
make.bottom.equalTo(self.editImageToolView)
179178
}
180179

181-
self.pickImageBtn = UIButton(type: .custom)
182-
self.pickImageBtn.backgroundColor = .black
183-
self.pickImageBtn.layer.cornerRadius = 5
184-
self.pickImageBtn.layer.masksToBounds = true
185-
self.pickImageBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
186-
self.pickImageBtn.setTitle("Pick an image", for: .normal)
187-
self.pickImageBtn.addTarget(self, action: #selector(pickImage), for: .touchUpInside)
188-
self.view.addSubview(self.pickImageBtn)
189-
self.pickImageBtn.snp.makeConstraints { (make) in
180+
pickImageBtn = UIButton(type: .custom)
181+
pickImageBtn.backgroundColor = .black
182+
pickImageBtn.layer.cornerRadius = 5
183+
pickImageBtn.layer.masksToBounds = true
184+
pickImageBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
185+
pickImageBtn.setTitle("Pick an image", for: .normal)
186+
pickImageBtn.addTarget(self, action: #selector(pickImage), for: .touchUpInside)
187+
view.addSubview(pickImageBtn)
188+
pickImageBtn.snp.makeConstraints { make in
190189
make.top.equalTo(self.editImageToolView.snp.bottom).offset(spacing)
191190
make.left.equalTo(self.editImageToolView)
192191
}
193192

194-
self.resultImageView = UIImageView()
195-
self.resultImageView.contentMode = .scaleAspectFit
196-
self.resultImageView.clipsToBounds = true
197-
self.view.addSubview(self.resultImageView)
198-
self.resultImageView.snp.makeConstraints { (make) in
193+
resultImageView = UIImageView()
194+
resultImageView.contentMode = .scaleAspectFit
195+
resultImageView.clipsToBounds = true
196+
view.addSubview(resultImageView)
197+
resultImageView.snp.makeConstraints { make in
199198
make.top.equalTo(self.pickImageBtn.snp.bottom).offset(spacing)
200199
make.left.right.equalTo(self.view)
201200
make.bottom.equalTo(self.view.snp.bottomMargin)
202201
}
203202

204203
let control = UIControl()
205204
control.addTarget(self, action: #selector(continueEditImage), for: .touchUpInside)
206-
self.view.addSubview(control)
207-
control.snp.makeConstraints { (make) in
205+
view.addSubview(control)
206+
control.snp.makeConstraints { make in
208207
make.edges.equalTo(self.resultImageView)
209208
}
210209
}
@@ -233,7 +232,7 @@ class ViewController: UIViewController {
233232
picker.delegate = self
234233
picker.sourceType = .photoLibrary
235234
picker.mediaTypes = ["public.image"]
236-
self.showDetailViewController(picker, sender: nil)
235+
showDetailViewController(picker, sender: nil)
237236
}
238237

239238
@objc func drawToolChanged() {
@@ -293,34 +292,31 @@ class ViewController: UIViewController {
293292
}
294293

295294
@objc func continueEditImage() {
296-
guard let oi = self.originalImage else {
295+
guard let oi = originalImage else {
297296
return
298297
}
299-
self.editImage(oi, editModel: self.resultImageEditModel)
298+
299+
editImage(oi, editModel: resultImageEditModel)
300300
}
301301

302302
func editImage(_ image: UIImage, editModel: ZLEditImageModel?) {
303-
ZLEditImageViewController.showEditImageVC(parentVC: self, image: image, editModel: editModel) { [weak self] (resImage, editModel) in
303+
ZLEditImageViewController.showEditImageVC(parentVC: self, image: image, editModel: editModel) { [weak self] resImage, editModel in
304304
self?.resultImageView.image = resImage
305305
self?.resultImageEditModel = editModel
306306
}
307307
}
308-
309308
}
310309

311-
312310
extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
313-
314311
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
315312
picker.dismiss(animated: true, completion: nil)
316313
}
317314

318-
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
315+
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
319316
picker.dismiss(animated: true) {
320317
guard let image = info[.originalImage] as? UIImage else { return }
321318
self.originalImage = image
322319
self.editImage(image, editModel: nil)
323320
}
324321
}
325-
326322
}

Sources/Extensions/UIImage+ZLImageEditor.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,20 +199,25 @@ extension ZLImageEditorWrapper where Base: UIImage {
199199
}
200200
}
201201

202-
func resize(_ size: CGSize) -> UIImage? {
202+
func resize(_ size: CGSize, scale: CGFloat? = nil) -> UIImage? {
203203
if size.width <= 0 || size.height <= 0 {
204204
return nil
205205
}
206-
UIGraphicsBeginImageContextWithOptions(size, false, base.scale)
206+
207+
UIGraphicsBeginImageContextWithOptions(size, false, scale ?? base.scale)
207208
base.draw(in: CGRect(origin: .zero, size: size))
208209
let temp = UIGraphicsGetImageFromCurrentImageContext()
209210
UIGraphicsEndImageContext()
210211
return temp
211212
}
212213

213-
/// Processing speed is better than resize(:) method
214-
/// bitsPerPixel = bitsPerComponent * 4
215-
func resize_vI(_ size: CGSize, bitsPerComponent: UInt32 = 8, bitsPerPixel: UInt32 = 32) -> UIImage? {
214+
/// Resize image. Processing speed is better than resize(:) method
215+
/// - Parameters:
216+
/// - size: Dest size of the image.
217+
/// - scale: The scale factor of the image.
218+
/// - bitsPerComponent: The number of bits allocated for a single color component of a bitmap image.
219+
/// - bitsPerPixel: The number of bits allocated for a single pixel in a bitmap image. bitsPerComponent * 4
220+
func resize_vI(_ size: CGSize, scale: CGFloat? = nil, bitsPerComponent: UInt32 = 8, bitsPerPixel: UInt32 = 32) -> UIImage? {
216221
guard let cgImage = base.cgImage else { return nil }
217222

218223
var format = vImage_CGImageFormat(
@@ -253,7 +258,7 @@ extension ZLImageEditorWrapper where Base: UIImage {
253258
guard error == kvImageNoError else { return nil }
254259

255260
// create a UIImage
256-
return UIImage(cgImage: destCGImage, scale: base.scale, orientation: base.imageOrientation)
261+
return UIImage(cgImage: destCGImage, scale: scale ?? base.scale, orientation: base.imageOrientation)
257262
}
258263

259264
func toCIImage() -> CIImage? {

Sources/General/ZLEditImageViewController.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,15 @@ open class ZLEditImageViewController: UIViewController {
348348
}
349349

350350
@objc public init(image: UIImage, editModel: ZLEditImageModel? = nil) {
351+
var image = image
352+
if image.scale != 1,
353+
let cgImage = image.cgImage {
354+
image = image.zl.resize_vI(
355+
CGSize(width: cgImage.width, height: cgImage.height),
356+
scale: 1
357+
) ?? image
358+
}
359+
351360
originalImage = image.zl.fixOrientation()
352361
editImage = originalImage
353362
editImageWithoutAdjust = originalImage

0 commit comments

Comments
 (0)