Skip to content

Commit bc24270

Browse files
committed
fixed ui issues
Updated example
1 parent 09fd69b commit bc24270

6 files changed

Lines changed: 125 additions & 15 deletions

File tree

Example/CropperExample/CropperExample.xcodeproj/project.pbxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
/* End PBXBuildFile section */
1818

1919
/* Begin PBXFileReference section */
20+
491DAAE820CA66A500A1C33A /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../../README.md; sourceTree = "<group>"; };
2021
49329A191FFB79F600741BBA /* UIImage+FixOrientation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = "UIImage+FixOrientation.swift"; path = "../../UIImageCropper/UIImage+FixOrientation.swift"; sourceTree = "<group>"; };
2122
49B955061FF5795B00BA9181 /* CropperExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CropperExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
2223
49B955091FF5795B00BA9181 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
@@ -46,6 +47,7 @@
4647
49B955071FF5795B00BA9181 /* Products */,
4748
49B9551D1FF57EBF00BA9181 /* UIImageCropper.swift */,
4849
49329A191FFB79F600741BBA /* UIImage+FixOrientation.swift */,
50+
491DAAE820CA66A500A1C33A /* README.md */,
4951
);
5052
sourceTree = "<group>";
5153
};

Example/CropperExample/CropperExample/Base.lproj/Main.storyboard

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,63 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13771" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14113" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="vkS-BR-BTR">
33
<device id="retina4_7" orientation="portrait">
44
<adaptation id="fullscreen"/>
55
</device>
66
<dependencies>
77
<deployment identifier="iOS"/>
8-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
8+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/>
99
<capability name="Aspect ratio constraints" minToolsVersion="5.1"/>
1010
<capability name="Constraints with non-1.0 multipliers" minToolsVersion="5.1"/>
1111
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
1212
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
1313
</dependencies>
1414
<scenes>
15+
<!--View Controller-->
16+
<scene sceneID="cVQ-Oq-57n">
17+
<objects>
18+
<viewController id="jNZ-Ml-f3B" sceneMemberID="viewController">
19+
<view key="view" contentMode="scaleToFill" id="p6z-j4-OHE">
20+
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
21+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
22+
<subviews>
23+
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="oXp-ad-NsJ">
24+
<rect key="frame" x="70" y="345" width="234" height="41"/>
25+
<fontDescription key="fontDescription" type="system" pointSize="24"/>
26+
<state key="normal" title="Open UIImageCropper"/>
27+
<connections>
28+
<segue destination="BYZ-38-t0r" kind="presentation" id="46V-tA-0dr"/>
29+
</connections>
30+
</button>
31+
</subviews>
32+
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
33+
<constraints>
34+
<constraint firstItem="oXp-ad-NsJ" firstAttribute="centerX" secondItem="cHE-q4-eRy" secondAttribute="centerX" id="tdZ-Py-DLm"/>
35+
<constraint firstItem="oXp-ad-NsJ" firstAttribute="centerY" secondItem="cHE-q4-eRy" secondAttribute="centerY" id="x7B-Ys-mMX"/>
36+
</constraints>
37+
<viewLayoutGuide key="safeArea" id="cHE-q4-eRy"/>
38+
</view>
39+
<navigationItem key="navigationItem" id="vmu-4y-Q9z"/>
40+
</viewController>
41+
<placeholder placeholderIdentifier="IBFirstResponder" id="qbq-vt-Xqb" userLabel="First Responder" sceneMemberID="firstResponder"/>
42+
</objects>
43+
<point key="canvasLocation" x="-679" y="132"/>
44+
</scene>
45+
<!--Navigation Controller-->
46+
<scene sceneID="7W4-7d-Tw3">
47+
<objects>
48+
<navigationController id="vkS-BR-BTR" sceneMemberID="viewController">
49+
<navigationBar key="navigationBar" contentMode="scaleToFill" insetsLayoutMarginsFromSafeArea="NO" id="NvZ-ev-1Iq">
50+
<rect key="frame" x="0.0" y="20" width="375" height="44"/>
51+
<autoresizingMask key="autoresizingMask"/>
52+
</navigationBar>
53+
<connections>
54+
<segue destination="jNZ-Ml-f3B" kind="relationship" relationship="rootViewController" id="v0u-Qc-Jhs"/>
55+
</connections>
56+
</navigationController>
57+
<placeholder placeholderIdentifier="IBFirstResponder" id="wNI-Dn-b0K" userLabel="First Responder" sceneMemberID="firstResponder"/>
58+
</objects>
59+
<point key="canvasLocation" x="-1521" y="132"/>
60+
</scene>
1561
<!--View Controller-->
1662
<scene sceneID="tne-QT-ifu">
1763
<objects>
@@ -40,14 +86,23 @@
4086
<action selector="cropExistingImage:" destination="BYZ-38-t0r" eventType="touchUpInside" id="EDz-Tg-E9f"/>
4187
</connections>
4288
</button>
89+
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="yO7-Mk-Sb5">
90+
<rect key="frame" x="16" y="28" width="39" height="30"/>
91+
<state key="normal" title="Close"/>
92+
<connections>
93+
<action selector="closeView:" destination="BYZ-38-t0r" eventType="touchUpInside" id="rxG-Pq-NkC"/>
94+
</connections>
95+
</button>
4396
</subviews>
4497
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
4598
<constraints>
99+
<constraint firstItem="yO7-Mk-Sb5" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" constant="16" id="2Jb-vl-sQm"/>
46100
<constraint firstItem="ONc-WP-Z8n" firstAttribute="centerX" secondItem="6Tk-OE-BBY" secondAttribute="centerX" id="2oG-lG-Nrt"/>
47101
<constraint firstItem="2hq-Uc-P8Z" firstAttribute="centerY" secondItem="6Tk-OE-BBY" secondAttribute="centerY" id="71j-ft-fZU"/>
48102
<constraint firstItem="2hq-Uc-P8Z" firstAttribute="width" secondItem="8bC-Xf-vdC" secondAttribute="width" multiplier="0.7" id="Cc7-Am-BSd"/>
49103
<constraint firstItem="UwF-md-sdr" firstAttribute="centerX" secondItem="6Tk-OE-BBY" secondAttribute="centerX" id="Mgz-ox-vub"/>
50104
<constraint firstItem="6Tk-OE-BBY" firstAttribute="bottom" secondItem="UwF-md-sdr" secondAttribute="bottom" constant="47" id="aUl-E9-3Lw"/>
105+
<constraint firstItem="yO7-Mk-Sb5" firstAttribute="top" secondItem="6Tk-OE-BBY" secondAttribute="top" constant="8" id="atd-fX-yej"/>
51106
<constraint firstItem="2hq-Uc-P8Z" firstAttribute="centerX" secondItem="6Tk-OE-BBY" secondAttribute="centerX" id="bBv-7P-YiA"/>
52107
<constraint firstItem="ONc-WP-Z8n" firstAttribute="top" secondItem="6Tk-OE-BBY" secondAttribute="top" constant="36" id="mxc-o0-kBt"/>
53108
</constraints>
@@ -59,6 +114,7 @@
59114
</viewController>
60115
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
61116
</objects>
117+
<point key="canvasLocation" x="316" y="131.78410794602701"/>
62118
</scene>
63119
</scenes>
64120
</document>

Example/CropperExample/CropperExample/ViewController.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,26 @@ class ViewController: UIViewController {
1919
super.viewDidLoad()
2020

2121
//setup the cropper
22-
cropper.picker = picker
2322
cropper.delegate = self
2423
//cropper.cropRatio = 2/3 //(can be set during runtime or in init)
2524
cropper.cropButtonText = "Crop" // this can be localized if needed (as well as the cancelButtonText)
2625
}
2726

27+
@IBAction func closeView(_ sender: Any) {
28+
self.dismiss(animated: true, completion: nil)
29+
}
30+
2831
@IBAction func cropExistingImage(_ sender: Any) {
32+
let cropper = UIImageCropper(cropRatio: 2/3)
33+
cropper.delegate = self
34+
cropper.picker = nil
2935
cropper.image = UIImage(named: "image")
3036
cropper.cancelButtonText = "Cancel"
3137
self.present(cropper, animated: true, completion: nil)
3238
}
3339

3440
@IBAction func takePicturePressed(_ sender: Any) {
41+
cropper.picker = picker
3542
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
3643

3744
cropper.cancelButtonText = "Retake"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ If just cropping existing UIImage there is no need to set up picker, delegate is
5353
Just give image to croppen and present it.
5454

5555
```
56+
cropper.picker = nil // Make sure not set the picker when doing existing image cropping
5657
cropper.image = UIImage(named: "image")
5758
viewController.present(cropper, animated: true, completion: nil)
5859
```

UIImageCropper.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'UIImageCropper'
11-
s.version = '1.3.3'
11+
s.version = '1.3.4'
1212
s.summary = 'Simple Image cropper for UIImage and UIImagePickerController with customisable aspect ratio.'
1313

1414
# This description is used to generate tags and improve search results.

UIImageCropper/UIImageCropper.swift

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -200,19 +200,20 @@ public class UIImageCropper: UIViewController, UIImagePickerControllerDelegate,
200200
return
201201
}
202202
layoutDone = true
203-
maskFadeView()
204-
203+
205204
if ratio < 1 {
206205
imageWidthConst?.constant = cropView.frame.height / ratio
207206
imageHeightConst?.constant = cropView.frame.height
208207
} else {
209208
imageWidthConst?.constant = cropView.frame.width
210209
imageHeightConst?.constant = cropView.frame.width * ratio
211210
}
212-
211+
213212
let horizontal = NSLayoutConstraint.constraints(withVisualFormat: "H:|-(<=\(cropView.frame.origin.x))-[view]-(<=\(cropView.frame.origin.x))-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["view": imageView])
214213
let vertical = NSLayoutConstraint.constraints(withVisualFormat: "V:|-(<=\(cropView.frame.origin.y))-[view]-(<=\(cropView.frame.origin.y))-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["view": imageView])
215214
topView.addConstraints(horizontal + vertical)
215+
216+
maskFadeView()
216217
orgWidth = imageWidthConst!.constant
217218
orgHeight = imageHeightConst!.constant
218219
}
@@ -228,16 +229,34 @@ public class UIImageCropper: UIViewController, UIImagePickerControllerDelegate,
228229

229230
//MARK: - button actions
230231
@objc func cropDone() {
231-
self.dismiss(animated: false, completion: {
232+
presenting = false
233+
if picker == nil {
234+
self.dismiss(animated: false, completion: {
235+
if self.autoClosePicker {
236+
self.picker?.dismiss(animated: true, completion: nil)
237+
}
238+
self.delegate?.didCropImage(originalImage: self.image, croppedImage: self.cropImage)
239+
})
240+
} else {
241+
self.endAppearanceTransition()
242+
self.view.removeFromSuperview()
243+
self.removeFromParentViewController()
232244
if self.autoClosePicker {
233245
self.picker?.dismiss(animated: true, completion: nil)
234246
}
235247
self.delegate?.didCropImage(originalImage: self.image, croppedImage: self.cropImage)
236-
})
248+
}
237249
}
238-
250+
239251
@objc func cropCancel() {
240-
self.dismiss(animated: true, completion: nil)
252+
presenting = false
253+
if picker == nil {
254+
self.dismiss(animated: true, completion: nil)
255+
} else {
256+
self.endAppearanceTransition()
257+
self.view.removeFromSuperview()
258+
self.removeFromParentViewController()
259+
}
241260
}
242261

243262
//MARK: - gesture handling
@@ -277,26 +296,51 @@ public class UIImageCropper: UIViewController, UIImagePickerControllerDelegate,
277296

278297
let cropFrame = CGRect(x: x * imageSize.width, y: y * imageSize.height, width: imageSize.width * width, height: imageSize.height * height)
279298
if let cropCGImage = image.cgImage?.cropping(to: cropFrame) {
280-
let cropImage = UIImage(cgImage: cropCGImage, scale: 1, orientation: .up)//(cgImage: cropCGImage,)
299+
let cropImage = UIImage(cgImage: cropCGImage, scale: 1, orientation: .up)
281300
return cropImage
282301
}
283302
return nil
284303
}
285304

286305
//MARK: - UIImagePickerControllerDelegates
287306
public func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
307+
presenting = false
288308
if delegate?.didCancel?() == nil {
289309
picker.dismiss(animated: true, completion: nil)
290310
}
291311
}
292-
312+
313+
var presenting = false
314+
293315
public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
316+
guard !presenting else {
317+
return
318+
}
294319
guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage else {
295320
return
296321
}
297322
layoutDone = false
298-
323+
presenting = true
299324
self.image = image.fixOrientation()
300-
self.picker?.present(self, animated: true, completion: nil)
325+
self.picker?.view.addSubview(self.view)
326+
self.view.constraintToFill(superView: self.picker?.view)
327+
self.picker?.addChildViewController(self)
328+
self.willMove(toParentViewController: self.picker)
329+
self.beginAppearanceTransition(true, animated: false)
330+
}
331+
332+
}
333+
334+
extension UIView {
335+
func constraintToFill(superView view: UIView?) {
336+
guard let view = view else {
337+
assertionFailure("superview is nil")
338+
return
339+
}
340+
self.translatesAutoresizingMaskIntoConstraints = false
341+
self.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
342+
self.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
343+
self.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
344+
self.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
301345
}
302346
}

0 commit comments

Comments
 (0)