diff --git a/Classes/WDCropBorderView.swift b/Classes/WDCropBorderView.swift index ef985c1..1e5ab77 100644 --- a/Classes/WDCropBorderView.swift +++ b/Classes/WDCropBorderView.swift @@ -11,32 +11,39 @@ import UIKit internal class WDCropBorderView: UIView { private let kNumberOfBorderHandles: CGFloat = 8 private let kHandleDiameter: CGFloat = 24 + private var lockAspectRatio: Bool + + convenience init(frame: CGRect, lockAspectRatio locked: Bool) { + self.init(frame: frame) + self.lockAspectRatio = locked + } override init(frame: CGRect) { + self.lockAspectRatio = false super.init(frame: frame) - self.backgroundColor = UIColor.clearColor() + self.backgroundColor = UIColor.clear } required init?(coder aDecoder: NSCoder) { + self.lockAspectRatio = false super.init(coder: aDecoder) - self.backgroundColor = UIColor.clearColor() + self.backgroundColor = UIColor.clear } - override func drawRect(rect: CGRect) { + override func draw(_ rect: CGRect) { let context = UIGraphicsGetCurrentContext() - CGContextSetStrokeColorWithColor(context, - UIColor(red: 1, green: 1, blue: 1, alpha: 0.5).CGColor) - CGContextSetLineWidth(context, 1.5) - CGContextAddRect(context, CGRectMake(kHandleDiameter / 2, kHandleDiameter / 2, - rect.size.width - kHandleDiameter, rect.size.height - kHandleDiameter)) - CGContextStrokePath(context) + context?.setStrokeColor(UIColor(red: 1, green: 1, blue: 1, alpha: 0.5).cgColor) + context?.setLineWidth(1.5) + context?.addRect(CGRect(x: kHandleDiameter / 2, y: kHandleDiameter / 2, + width: rect.size.width - kHandleDiameter, height: rect.size.height - kHandleDiameter)) + context?.strokePath() - CGContextSetRGBFillColor(context, 1, 1, 1, 0.95) + context?.setFillColor(red: 1, green: 1, blue: 1, alpha: 0.95) for handleRect in calculateAllNeededHandleRects() { - CGContextFillEllipseInRect(context, handleRect) + context?.fillEllipse(in: handleRect) } } @@ -53,17 +60,19 @@ internal class WDCropBorderView: UIView { let bottomRowY = height - kHandleDiameter let middleRowY = bottomRowY / 2 - //starting with the upper left corner and then following clockwise - let topLeft = CGRectMake(leftColX, topRowY, kHandleDiameter, kHandleDiameter) - let topCenter = CGRectMake(centerColX, topRowY, kHandleDiameter, kHandleDiameter) - let topRight = CGRectMake(rightColX, topRowY, kHandleDiameter, kHandleDiameter) - let middleRight = CGRectMake(rightColX, middleRowY, kHandleDiameter, kHandleDiameter) - let bottomRight = CGRectMake(rightColX, bottomRowY, kHandleDiameter, kHandleDiameter) - let bottomCenter = CGRectMake(centerColX, bottomRowY, kHandleDiameter, kHandleDiameter) - let bottomLeft = CGRectMake(leftColX, bottomRowY, kHandleDiameter, kHandleDiameter) - let middleLeft = CGRectMake(leftColX, middleRowY, kHandleDiameter, kHandleDiameter) - - return [topLeft, topCenter, topRight, middleRight, bottomRight, bottomCenter, bottomLeft, - middleLeft] + var handleArray = [CGRect]() + handleArray.append(CGRect(x: leftColX, y: topRowY, width: kHandleDiameter, height: kHandleDiameter)) //top left + handleArray.append(CGRect(x: rightColX, y: topRowY, width: kHandleDiameter, height: kHandleDiameter)) //top right + handleArray.append(CGRect(x: rightColX, y: bottomRowY, width: kHandleDiameter, height: kHandleDiameter)) //bottom right + handleArray.append(CGRect(x: leftColX, y: bottomRowY, width: kHandleDiameter, height: kHandleDiameter)) //bottom left + + if !lockAspectRatio { + handleArray.append(CGRect(x: centerColX, y: topRowY, width: kHandleDiameter, height: kHandleDiameter)) //top center + handleArray.append(CGRect(x: rightColX, y: middleRowY, width: kHandleDiameter, height: kHandleDiameter)) //middle right + handleArray.append(CGRect(x: centerColX, y: bottomRowY, width: kHandleDiameter, height: kHandleDiameter)) //bottom center + handleArray.append(CGRect(x: leftColX, y: middleRowY, width: kHandleDiameter, height: kHandleDiameter)) //middle left + } + + return handleArray } -} \ No newline at end of file +} diff --git a/Classes/WDImageCropOverlayView.swift b/Classes/WDImageCropOverlayView.swift index eebf0f2..6aea803 100644 --- a/Classes/WDImageCropOverlayView.swift +++ b/Classes/WDImageCropOverlayView.swift @@ -16,23 +16,23 @@ internal class WDImageCropOverlayView: UIView { override init(frame: CGRect) { super.init(frame: frame) - self.backgroundColor = UIColor.clearColor() - self.userInteractionEnabled = true + self.backgroundColor = UIColor.clear + self.isUserInteractionEnabled = true } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) - self.backgroundColor = UIColor.clearColor() - self.userInteractionEnabled = true + self.backgroundColor = UIColor.clear + self.isUserInteractionEnabled = true } - override func drawRect(rect: CGRect) { + override func draw(_ rect: CGRect) { - let toolbarSize = CGFloat(UIDevice.currentDevice().userInterfaceIdiom == .Pad ? 0 : 54) + let toolbarSize = CGFloat(UIDevice.current.userInterfaceIdiom == .pad ? 0 : 54) - let width = CGRectGetWidth(self.frame) - let height = CGRectGetHeight(self.frame) - toolbarSize + let width = self.frame.width + let height = self.frame.height - toolbarSize let heightSpan = floor(height / 2 - self.cropSize.height / 2) let widthSpan = floor(width / 2 - self.cropSize.width / 2) @@ -43,11 +43,11 @@ internal class WDImageCropOverlayView: UIView { // fill inner border UIColor(red: 1, green: 1, blue: 1, alpha: 0.5).set() - UIRectFrame(CGRectMake(widthSpan - 2, heightSpan - 2, self.cropSize.width + 4, - self.cropSize.height + 4)) + UIRectFrame(CGRect(x: widthSpan - 2, y: heightSpan - 2, width: self.cropSize.width + 4, + height: self.cropSize.height + 4)) // fill inner rect - UIColor.clearColor().set() - UIRectFill(CGRectMake(widthSpan, heightSpan, self.cropSize.width, self.cropSize.height)) + UIColor.clear.set() + UIRectFill(CGRect(x: widthSpan, y: heightSpan, width: self.cropSize.width, height: self.cropSize.height)) } } diff --git a/Classes/WDImageCropView.swift b/Classes/WDImageCropView.swift index 7158dfe..b48c836 100644 --- a/Classes/WDImageCropView.swift +++ b/Classes/WDImageCropView.swift @@ -13,7 +13,7 @@ private class ScrollView: UIScrollView { private override func layoutSubviews() { super.layoutSubviews() - if let zoomView = self.delegate?.viewForZoomingInScrollView?(self) { + if let zoomView = self.delegate?.viewForZooming?(in: self) { let boundsSize = self.bounds.size var frameToCenter = zoomView.frame @@ -38,6 +38,7 @@ private class ScrollView: UIScrollView { internal class WDImageCropView: UIView, UIScrollViewDelegate { var resizableCropArea = false + var lockAspectRatio = false private var scrollView: UIScrollView! private var imageView: UIImageView! @@ -45,12 +46,12 @@ internal class WDImageCropView: UIView, UIScrollViewDelegate { private var xOffset: CGFloat! private var yOffset: CGFloat! - private static func scaleRect(rect: CGRect, scale: CGFloat) -> CGRect { - return CGRectMake( - rect.origin.x * scale, - rect.origin.y * scale, - rect.size.width * scale, - rect.size.height * scale) + private static func scaleRect(_ rect: CGRect, scale: CGFloat) -> CGRect { + return CGRect( + x: rect.origin.x * scale, + y: rect.origin.y * scale, + width: rect.size.width * scale, + height: rect.size.height * scale) } var imageToCrop: UIImage? { @@ -72,7 +73,7 @@ internal class WDImageCropView: UIView, UIScrollViewDelegate { } else { if self.resizableCropArea { self.cropOverlayView = WDResizableCropOverlayView(frame: self.bounds, - initialContentSize: CGSizeMake(newValue.width, newValue.height)) + initialContentSize: CGSize(width: newValue.width, height: newValue.height), lockAspectRatio: self.lockAspectRatio) } else { self.cropOverlayView = WDImageCropOverlayView(frame: self.bounds) } @@ -85,24 +86,24 @@ internal class WDImageCropView: UIView, UIScrollViewDelegate { override init(frame: CGRect) { super.init(frame: frame) - self.userInteractionEnabled = true - self.backgroundColor = UIColor.blackColor() + self.isUserInteractionEnabled = true + self.backgroundColor = UIColor.black self.scrollView = ScrollView(frame: frame) self.scrollView.showsHorizontalScrollIndicator = false self.scrollView.showsVerticalScrollIndicator = false self.scrollView.delegate = self self.scrollView.clipsToBounds = false self.scrollView.decelerationRate = 0 - self.scrollView.backgroundColor = UIColor.clearColor() + self.scrollView.backgroundColor = UIColor.clear self.addSubview(self.scrollView) self.imageView = UIImageView(frame: self.scrollView.frame) - self.imageView.contentMode = .ScaleAspectFit - self.imageView.backgroundColor = UIColor.blackColor() + self.imageView.contentMode = .scaleAspectFit + self.imageView.backgroundColor = UIColor.black self.scrollView.addSubview(self.imageView) self.scrollView.minimumZoomScale = - CGRectGetWidth(self.scrollView.frame) / CGRectGetHeight(self.scrollView.frame) + self.scrollView.frame.width / self.scrollView.frame.height self.scrollView.maximumZoomScale = 20 self.scrollView.setZoomScale(1.0, animated: false) } @@ -111,31 +112,31 @@ internal class WDImageCropView: UIView, UIScrollViewDelegate { super.init(coder: aDecoder) } - override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? { + override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { if !resizableCropArea { return self.scrollView } let resizableCropView = cropOverlayView as! WDResizableCropOverlayView - let outerFrame = CGRectInset(resizableCropView.cropBorderView.frame, -10, -10) + let outerFrame = resizableCropView.cropBorderView.frame.insetBy(dx: -10, dy: -10) - if CGRectContainsPoint(outerFrame, point) { + if outerFrame.contains(point) { if resizableCropView.cropBorderView.frame.size.width < 60 || resizableCropView.cropBorderView.frame.size.height < 60 { - return super.hitTest(point, withEvent: event) + return super.hitTest(point, with: event) } - let innerTouchFrame = CGRectInset(resizableCropView.cropBorderView.frame, 30, 30) - if CGRectContainsPoint(innerTouchFrame, point) { + let innerTouchFrame = resizableCropView.cropBorderView.frame.insetBy(dx: 30, dy: 30) + if innerTouchFrame.contains(point) { return self.scrollView } - let outBorderTouchFrame = CGRectInset(resizableCropView.cropBorderView.frame, -10, -10) - if CGRectContainsPoint(outBorderTouchFrame, point) { - return super.hitTest(point, withEvent: event) + let outBorderTouchFrame = resizableCropView.cropBorderView.frame.insetBy(dx: -10, dy: -10) + if outBorderTouchFrame.contains(point) { + return super.hitTest(point, with: event) } - return super.hitTest(point, withEvent: event) + return super.hitTest(point, with: event) } return self.scrollView @@ -145,9 +146,9 @@ internal class WDImageCropView: UIView, UIScrollViewDelegate { super.layoutSubviews() let size = self.cropSize; - let toolbarSize = CGFloat(UIDevice.currentDevice().userInterfaceIdiom == .Pad ? 0 : 54) - self.xOffset = floor((CGRectGetWidth(self.bounds) - size.width) * 0.5) - self.yOffset = floor((CGRectGetHeight(self.bounds) - toolbarSize - size.height) * 0.5) + let toolbarSize = CGFloat(UIDevice.current.userInterfaceIdiom == .pad ? 0 : 54) + self.xOffset = floor((self.bounds.width - size.width) * 0.5) + self.yOffset = floor((self.bounds.height - toolbarSize - size.height) * 0.5) let height = self.imageToCrop!.size.height let width = self.imageToCrop!.size.width @@ -167,31 +168,26 @@ internal class WDImageCropView: UIView, UIScrollViewDelegate { } self.cropOverlayView.frame = self.bounds - self.scrollView.frame = CGRectMake(xOffset, yOffset, size.width, size.height) - self.scrollView.contentSize = CGSizeMake(size.width, size.height) - self.imageView.frame = CGRectMake(0, floor((size.height - factoredHeight) * 0.5), - factoredWidth, factoredHeight) + self.scrollView.frame = CGRect(x: xOffset, y: yOffset, width: size.width, height: size.height) + self.scrollView.contentSize = CGSize(width: size.width, height: size.height) + self.imageView.frame = CGRect(x: 0, y: floor((size.height - factoredHeight) * 0.5), + width: factoredWidth, height: factoredHeight) } - func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? { + func viewForZooming(in scrollView: UIScrollView) -> UIView? { return self.imageView } - func croppedImage() -> UIImage { + func croppedImage() -> CGRect { // Calculate rect that needs to be cropped var visibleRect = resizableCropArea ? calcVisibleRectForResizeableCropArea() : calcVisibleRectForCropArea() // transform visible rect to image orientation let rectTransform = orientationTransformedRectOfImage(imageToCrop!) - visibleRect = CGRectApplyAffineTransform(visibleRect, rectTransform); + visibleRect = visibleRect.applying(rectTransform); - // finally crop image - let imageRef = CGImageCreateWithImageInRect(imageToCrop!.CGImage, visibleRect) - let result = UIImage(CGImage: imageRef!, scale: imageToCrop!.scale, - orientation: imageToCrop!.imageOrientation) - - return result + return visibleRect } private func calcVisibleRectForResizeableCropArea() -> CGRect { @@ -204,8 +200,8 @@ internal class WDImageCropView: UIView, UIScrollViewDelegate { sizeScale *= self.scrollView.zoomScale // then get the postion of the cropping rect inside the image - var visibleRect = resizableView.contentView.convertRect(resizableView.contentView.bounds, - toView: imageView) + var visibleRect = resizableView.contentView.convert(resizableView.contentView.bounds, + to: imageView) visibleRect = WDImageCropView.scaleRect(visibleRect, scale: sizeScale) return visibleRect @@ -230,30 +226,26 @@ internal class WDImageCropView: UIView, UIScrollViewDelegate { } // extract visible rect from scrollview and scale it - var visibleRect = scrollView.convertRect(scrollView.bounds, toView:imageView) + var visibleRect = scrollView.convert(scrollView.bounds, to:imageView) visibleRect = WDImageCropView.scaleRect(visibleRect, scale: scale) return visibleRect } - private func orientationTransformedRectOfImage(image: UIImage) -> CGAffineTransform { + private func orientationTransformedRectOfImage(_ image: UIImage) -> CGAffineTransform { var rectTransform: CGAffineTransform! switch image.imageOrientation { - case .Left: - rectTransform = CGAffineTransformTranslate( - CGAffineTransformMakeRotation(CGFloat(M_PI_2)), 0, -image.size.height) - case .Right: - rectTransform = CGAffineTransformTranslate( - CGAffineTransformMakeRotation(CGFloat(-M_PI_2)),-image.size.width, 0) - case .Down: - rectTransform = CGAffineTransformTranslate( - CGAffineTransformMakeRotation(CGFloat(-M_PI)), - -image.size.width, -image.size.height) + case .left: + rectTransform = CGAffineTransform(rotationAngle: CGFloat(M_PI_2)).translatedBy(x: 0, y: -image.size.height) + case .right: + rectTransform = CGAffineTransform(rotationAngle: CGFloat(-M_PI_2)).translatedBy(x: -image.size.width, y: 0) + case .down: + rectTransform = CGAffineTransform(rotationAngle: CGFloat(-M_PI)).translatedBy(x: -image.size.width, y: -image.size.height) default: - rectTransform = CGAffineTransformIdentity + rectTransform = CGAffineTransform.identity } - return CGAffineTransformScale(rectTransform, image.scale, image.scale) + return rectTransform.scaledBy(x: image.scale, y: image.scale) } -} \ No newline at end of file +} diff --git a/Classes/WDImageCropViewController.swift b/Classes/WDImageCropViewController.swift index b244791..4ed0618 100644 --- a/Classes/WDImageCropViewController.swift +++ b/Classes/WDImageCropViewController.swift @@ -10,7 +10,7 @@ import UIKit import CoreGraphics internal protocol WDImageCropControllerDelegate { - func imageCropController(imageCropController: WDImageCropViewController, didFinishWithCroppedImage croppedImage: UIImage) + func imageCropController(_ imageCropController: WDImageCropViewController, didFinishWithCroppedRect croppedRect: CGRect) } internal class WDImageCropViewController: UIViewController { @@ -18,8 +18,9 @@ internal class WDImageCropViewController: UIViewController { var delegate: WDImageCropControllerDelegate? var cropSize: CGSize! var resizableCropArea = false + var lockAspectRatio = false - private var croppedImage: UIImage! + private var croppedRect: CGRect! private var imageCropView: WDImageCropView! private var toolbar: UIToolbar! @@ -37,10 +38,10 @@ internal class WDImageCropViewController: UIViewController { self.setupCropView() self.setupToolbar() - if UIDevice.currentDevice().userInterfaceIdiom == .Phone { - self.navigationController?.navigationBarHidden = true + if UIDevice.current.userInterfaceIdiom == .phone { + self.navigationController?.isNavigationBarHidden = true } else { - self.navigationController?.navigationBarHidden = false + self.navigationController?.isNavigationBarHidden = false } } @@ -48,23 +49,23 @@ internal class WDImageCropViewController: UIViewController { super.viewWillLayoutSubviews() self.imageCropView.frame = self.view.bounds - self.toolbar?.frame = CGRectMake(0, CGRectGetHeight(self.view.frame) - 54, - self.view.frame.size.width, 54) + self.toolbar?.frame = CGRect(x: 0, y: self.view.frame.height - 54, + width: self.view.frame.size.width, height: 54) } - func actionCancel(sender: AnyObject) { - self.navigationController?.popViewControllerAnimated(true) + func actionCancel(_ sender: AnyObject) { + self.dismiss(animated: true, completion: nil) } - func actionUse(sender: AnyObject) { - croppedImage = self.imageCropView.croppedImage() - self.delegate?.imageCropController(self, didFinishWithCroppedImage: croppedImage) + func actionUse(_ sender: AnyObject) { + croppedRect = self.imageCropView.croppedImage() + self.delegate?.imageCropController(self, didFinishWithCroppedRect: croppedRect) } private func setupNavigationBar() { - self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Cancel, + self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(actionCancel)) - self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Use", style: .Plain, + self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Use", style: .plain, target: self, action: #selector(actionUse)) } @@ -72,71 +73,72 @@ internal class WDImageCropViewController: UIViewController { self.imageCropView = WDImageCropView(frame: self.view.bounds) self.imageCropView.imageToCrop = sourceImage self.imageCropView.resizableCropArea = self.resizableCropArea + self.imageCropView.lockAspectRatio = self.lockAspectRatio self.imageCropView.cropSize = cropSize self.view.addSubview(self.imageCropView) } private func setupCancelButton() { self.cancelButton = UIButton() - self.cancelButton.titleLabel?.font = UIFont.boldSystemFontOfSize(16) - self.cancelButton.titleLabel?.shadowOffset = CGSizeMake(0, -1) - self.cancelButton.frame = CGRectMake(0, 0, 58, 30) - self.cancelButton.setTitle("Cancel", forState: .Normal) + self.cancelButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16) + self.cancelButton.titleLabel?.shadowOffset = CGSize(width: 0, height: -1) + self.cancelButton.frame = CGRect(x: 0, y: 0, width: 58, height: 30) + self.cancelButton.setTitle("Cancel", for: UIControlState()) self.cancelButton.setTitleShadowColor( - UIColor(red: 0.118, green: 0.247, blue: 0.455, alpha: 1), forState: .Normal) - self.cancelButton.addTarget(self, action: #selector(actionCancel), forControlEvents: .TouchUpInside) + UIColor(red: 0.118, green: 0.247, blue: 0.455, alpha: 1), for: UIControlState()) + self.cancelButton.addTarget(self, action: #selector(actionCancel), for: .touchUpInside) } private func setupUseButton() { self.useButton = UIButton() - self.useButton.titleLabel?.font = UIFont.boldSystemFontOfSize(16) - self.useButton.titleLabel?.shadowOffset = CGSizeMake(0, -1) - self.useButton.frame = CGRectMake(0, 0, 58, 30) - self.useButton.setTitle("Use", forState: .Normal) + self.useButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16) + self.useButton.titleLabel?.shadowOffset = CGSize(width: 0, height: -1) + self.useButton.frame = CGRect(x: 0, y: 0, width: 58, height: 30) + self.useButton.setTitle("Use", for: UIControlState()) self.useButton.setTitleShadowColor( - UIColor(red: 0.118, green: 0.247, blue: 0.455, alpha: 1), forState: .Normal) - self.useButton.addTarget(self, action: #selector(actionUse), forControlEvents: .TouchUpInside) + UIColor(red: 0.118, green: 0.247, blue: 0.455, alpha: 1), for: UIControlState()) + self.useButton.addTarget(self, action: #selector(actionUse), for: .touchUpInside) } private func toolbarBackgroundImage() -> UIImage { let components: [CGFloat] = [1, 1, 1, 1, 123.0 / 255.0, 125.0 / 255.0, 132.0 / 255.0, 1] - UIGraphicsBeginImageContextWithOptions(CGSizeMake(320, 54), true, 0) + UIGraphicsBeginImageContextWithOptions(CGSize(width: 320, height: 54), true, 0) let context = UIGraphicsGetCurrentContext() let colorSpace = CGColorSpaceCreateDeviceRGB() - let gradient = CGGradientCreateWithColorComponents(colorSpace, components, nil, 2) + let gradient = CGGradient(colorComponentsSpace: colorSpace, components: components, locations: nil, count: 2) - CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(0, 54), []) + context?.drawLinearGradient(gradient!, start: CGPoint(x: 0, y: 0), end: CGPoint(x: 0, y: 54), options: []) let viewImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() - return viewImage + return viewImage! } private func setupToolbar() { - if UIDevice.currentDevice().userInterfaceIdiom == .Phone { - self.toolbar = UIToolbar(frame: CGRectZero) - self.toolbar.translucent = true - self.toolbar.barStyle = .Black + if UIDevice.current.userInterfaceIdiom == .phone { + self.toolbar = UIToolbar(frame: CGRect.zero) + self.toolbar.isTranslucent = true + self.toolbar.barStyle = .black self.view.addSubview(self.toolbar) self.setupCancelButton() self.setupUseButton() - let info = UILabel(frame: CGRectZero) + let info = UILabel(frame: CGRect.zero) info.text = "" info.textColor = UIColor(red: 0.173, green: 0.173, blue: 0.173, alpha: 1) - info.backgroundColor = UIColor.clearColor() + info.backgroundColor = UIColor.clear info.shadowColor = UIColor(red: 0.827, green: 0.731, blue: 0.839, alpha: 1) - info.shadowOffset = CGSizeMake(0, 1) - info.font = UIFont.boldSystemFontOfSize(18) + info.shadowOffset = CGSize(width: 0, height: 1) + info.font = UIFont.boldSystemFont(ofSize: 18) info.sizeToFit() let cancel = UIBarButtonItem(customView: self.cancelButton) - let flex = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil) + let flex = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil) let label = UIBarButtonItem(customView: info) let use = UIBarButtonItem(customView: self.useButton) diff --git a/Classes/WDImagePicker.swift b/Classes/WDImagePicker.swift index 70129de..1ac0a52 100644 --- a/Classes/WDImagePicker.swift +++ b/Classes/WDImagePicker.swift @@ -9,16 +9,21 @@ import UIKit @objc public protocol WDImagePickerDelegate { - optional func imagePicker(imagePicker: WDImagePicker, pickedImage: UIImage) - optional func imagePickerDidCancel(imagePicker: WDImagePicker) + @objc optional func imagePicker(_ imagePicker: WDImagePicker, pickedImage: UIImage) + //commented out while SR-2268 gets resolved + //@objc optional func imagePicker(_ imagePicker: WDImagePicker, pickedRect: CGRect, onImage: UIImage) + var prefersPickedImageRectangle: Bool { get } + @objc optional func imagePickerDidCancel(_ imagePicker: WDImagePicker) } @objc public class WDImagePicker: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate, WDImageCropControllerDelegate { public var delegate: WDImagePickerDelegate? public var cropSize: CGSize! public var resizableCropArea = false + public var lockAspectRatio = false private var _imagePickerController: UIImagePickerController! + private var chosenImage: UIImage? public var imagePickerController: UIImagePickerController { return _imagePickerController @@ -27,17 +32,17 @@ import UIKit override public init() { super.init() - self.cropSize = CGSizeMake(320, 320) + self.cropSize = CGSize(width: 320, height: 320) _imagePickerController = UIImagePickerController() _imagePickerController.delegate = self - _imagePickerController.sourceType = .PhotoLibrary + _imagePickerController.sourceType = .photoLibrary } private func hideController() { - self._imagePickerController.dismissViewControllerAnimated(true, completion: nil) + self._imagePickerController.dismiss(animated: true, completion: nil) } - public func imagePickerControllerDidCancel(picker: UIImagePickerController) { + public func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { if self.delegate?.imagePickerDidCancel != nil { self.delegate?.imagePickerDidCancel!(self) } else { @@ -45,16 +50,29 @@ import UIKit } } - public func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { + public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { let cropController = WDImageCropViewController() - cropController.sourceImage = info[UIImagePickerControllerOriginalImage] as! UIImage + chosenImage = (info[UIImagePickerControllerOriginalImage] as! UIImage) + cropController.sourceImage = chosenImage! cropController.resizableCropArea = self.resizableCropArea + cropController.lockAspectRatio = self.lockAspectRatio cropController.cropSize = self.cropSize cropController.delegate = self picker.pushViewController(cropController, animated: true) } - func imageCropController(imageCropController: WDImageCropViewController, didFinishWithCroppedImage croppedImage: UIImage) { - self.delegate?.imagePicker?(self, pickedImage: croppedImage) + func imageCropController(_ imageCropController: WDImageCropViewController, didFinishWithCroppedRect croppedRect: CGRect) { + guard let chosenImage = chosenImage else { return } //should never happen, unless this function is called outside of WDImagePicker's process + if let rectInsteadOfImage = delegate?.prefersPickedImageRectangle, rectInsteadOfImage == true { + //commented out while SR-2268 gets resolved + //self.delegate?.imagePicker?(self, pickedRect: croppedRect, onImage: chosenImage) + } + else { + // finally crop image + let imageRef = chosenImage.cgImage?.cropping(to: croppedRect) + let croppedImage = UIImage(cgImage: imageRef!, scale: chosenImage.scale, + orientation: chosenImage.imageOrientation) + self.delegate?.imagePicker?(self, pickedImage: croppedImage) + } } } diff --git a/Classes/WDResizableCropOverlayView.swift b/Classes/WDResizableCropOverlayView.swift index b877235..d75420c 100644 --- a/Classes/WDResizableCropOverlayView.swift +++ b/Classes/WDResizableCropOverlayView.swift @@ -26,6 +26,7 @@ internal class WDResizableCropOverlayView: WDImageCropOverlayView { private var anchor: CGPoint! private var startPoint: CGPoint! private var resizeMultiplyer = WDResizableViewBorderMultiplyer() + private var lockAspectRatio: Bool = false override var frame: CGRect { get { @@ -34,27 +35,27 @@ internal class WDResizableCropOverlayView: WDImageCropOverlayView { set { super.frame = newValue - let toolbarSize = CGFloat(UIDevice.currentDevice().userInterfaceIdiom == .Pad ? 0 : 54) + let toolbarSize = CGFloat(UIDevice.current.userInterfaceIdiom == .pad ? 0 : 54) let width = self.bounds.size.width let height = self.bounds.size.height - contentView?.frame = CGRectMake(( + contentView?.frame = CGRect(x: ( width - initialContentSize.width) / 2, - (height - toolbarSize - initialContentSize.height) / 2, - initialContentSize.width, - initialContentSize.height) - - cropBorderView?.frame = CGRectMake( - (width - initialContentSize.width) / 2 - kBorderCorrectionValue, - (height - toolbarSize - initialContentSize.height) / 2 - kBorderCorrectionValue, - initialContentSize.width + kBorderCorrectionValue * 2, - initialContentSize.height + kBorderCorrectionValue * 2) + y: (height - toolbarSize - initialContentSize.height) / 2, + width: initialContentSize.width, + height: initialContentSize.height) + + cropBorderView?.frame = CGRect( + x: (width - initialContentSize.width) / 2 - kBorderCorrectionValue, + y: (height - toolbarSize - initialContentSize.height) / 2 - kBorderCorrectionValue, + width: initialContentSize.width + kBorderCorrectionValue * 2, + height: initialContentSize.height + kBorderCorrectionValue * 2) } } - init(frame: CGRect, initialContentSize: CGSize) { + init(frame: CGRect, initialContentSize: CGSize, lockAspectRatio locked: Bool = false) { super.init(frame: frame) - + self.lockAspectRatio = locked self.initialContentSize = initialContentSize self.addContentViews() } @@ -67,58 +68,58 @@ internal class WDResizableCropOverlayView: WDImageCropOverlayView { super.init(frame: frame) } - override func touchesBegan(touches: Set, withEvent event: UIEvent?) { + override func touchesBegan(_ touches: Set, with event: UIEvent?) { if let touch = touches.first { - let touchPoint = touch.locationInView(cropBorderView) + let touchPoint = touch.location(in: cropBorderView) anchor = self.calculateAnchorBorder(touchPoint) fillMultiplyer() resizingEnabled = true - startPoint = touch.locationInView(self.superview) + startPoint = touch.location(in: self.superview) } } - override func touchesMoved(touches: Set, withEvent event: UIEvent?) { + override func touchesMoved(_ touches: Set, with event: UIEvent?) { if let touch = touches.first { if resizingEnabled! { - self.resizeWithTouchPoint(touch.locationInView(self.superview)) + self.resizeWithTouchPoint(touch.location(in: self.superview)) } } } - override func drawRect(rect: CGRect) { + override func draw(_ rect: CGRect) { //fill outer rect UIColor(red: 0, green: 0, blue: 0, alpha: 0.5).set() UIRectFill(self.bounds) //fill inner rect - UIColor.clearColor().set() + UIColor.clear.set() UIRectFill(self.contentView.frame) } private func addContentViews() { - let toolbarSize = CGFloat(UIDevice.currentDevice().userInterfaceIdiom == .Pad ? 0 : 54) + let toolbarSize = CGFloat(UIDevice.current.userInterfaceIdiom == .pad ? 0 : 54) let width = self.bounds.size.width let height = self.bounds.size.height - contentView = UIView(frame: CGRectMake(( + contentView = UIView(frame: CGRect(x: ( width - initialContentSize.width) / 2, - (height - toolbarSize - initialContentSize.height) / 2, - initialContentSize.width, - initialContentSize.height)) - contentView.backgroundColor = UIColor.clearColor() + y: (height - toolbarSize - initialContentSize.height) / 2, + width: initialContentSize.width, + height: initialContentSize.height)) + contentView.backgroundColor = UIColor.clear self.cropSize = contentView.frame.size self.addSubview(contentView) - cropBorderView = WDCropBorderView(frame: CGRectMake( - (width - initialContentSize.width) / 2 - kBorderCorrectionValue, - (height - toolbarSize - initialContentSize.height) / 2 - kBorderCorrectionValue, - initialContentSize.width + kBorderCorrectionValue * 2, - initialContentSize.height + kBorderCorrectionValue * 2)) + cropBorderView = WDCropBorderView(frame: CGRect( + x: (width - initialContentSize.width) / 2 - kBorderCorrectionValue, + y: (height - toolbarSize - initialContentSize.height) / 2 - kBorderCorrectionValue, + width: initialContentSize.width + kBorderCorrectionValue * 2, + height: initialContentSize.height + kBorderCorrectionValue * 2), lockAspectRatio: self.lockAspectRatio) self.addSubview(cropBorderView) } - private func calculateAnchorBorder(anchorPoint: CGPoint) -> CGPoint { + private func calculateAnchorBorder(_ anchorPoint: CGPoint) -> CGPoint { let allHandles = getAllCurrentHandlePositions() var closest: CGFloat = 3000 var anchor: CGPoint! @@ -145,23 +146,26 @@ internal class WDResizableCropOverlayView: WDImageCropOverlayView { let bottomY = cropBorderView.bounds.size.height let middleY = topY + (bottomY - topY) / 2 - // starting with the upper left corner and then following the rect clockwise - let topLeft = CGPointMake(leftX, topY) - let topCenter = CGPointMake(centerX, topY) - let topRight = CGPointMake(rightX, topY) - let middleRight = CGPointMake(rightX, middleY) - let bottomRight = CGPointMake(rightX, bottomY) - let bottomCenter = CGPointMake(centerX, bottomY) - let bottomLeft = CGPointMake(leftX, bottomY) - let middleLeft = CGPointMake(leftX, middleY) - - return [topLeft, topCenter, topRight, middleRight, bottomRight, bottomCenter, bottomLeft, - middleLeft] + var handleArray = [CGPoint]() + handleArray.append(CGPoint(x: leftX, y: topY)) //top left + handleArray.append(CGPoint(x: rightX, y: topY)) //top right + handleArray.append(CGPoint(x: rightX, y: bottomY)) //bottom right + handleArray.append(CGPoint(x: leftX, y: bottomY)) //bottom left + + if !lockAspectRatio { + handleArray.append(CGPoint(x: centerX, y: topY)) //top center + handleArray.append(CGPoint(x: rightX, y: middleY)) //middle right + handleArray.append(CGPoint(x: centerX, y: bottomY)) //bottom center + handleArray.append(CGPoint(x: leftX, y: middleY)) //middle left + } + + return handleArray } - private func resizeWithTouchPoint(point: CGPoint) { + private func resizeWithTouchPoint(_ point: CGPoint) { // This is the place where all the magic happends // prevent goint offscreen... + let border = kBorderCorrectionValue * 2 var pointX = point.x < border ? border : point.x var pointY = point.y < border ? border : point.y @@ -170,53 +174,92 @@ internal class WDResizableCropOverlayView: WDImageCropOverlayView { pointY = pointY > self.superview!.bounds.size.height - border ? self.superview!.bounds.size.height - border : pointY - let heightChange = (pointY - startPoint.y) * resizeMultiplyer.heightMultiplyer - let widthChange = (startPoint.x - pointX) * resizeMultiplyer.widthMultiplyer + var heightChange = (pointY - startPoint.y) * resizeMultiplyer.heightMultiplyer + var widthChange = (startPoint.x - pointX) * resizeMultiplyer.widthMultiplyer + + if lockAspectRatio { + let averageChange = (heightChange + widthChange) / 2 + heightChange = averageChange + widthChange = averageChange + } + let xChange = -1 * widthChange * resizeMultiplyer.xMultiplyer let yChange = -1 * heightChange * resizeMultiplyer.yMultiplyer - var newFrame = CGRectMake( - cropBorderView.frame.origin.x + xChange, - cropBorderView.frame.origin.y + yChange, - cropBorderView.frame.size.width + widthChange, - cropBorderView.frame.size.height + heightChange); + + var newFrame = CGRect( + x: cropBorderView.frame.origin.x + xChange, + y: cropBorderView.frame.origin.y + yChange, + width: cropBorderView.frame.size.width + widthChange, + height: cropBorderView.frame.size.height + heightChange); newFrame = self.preventBorderFrameFromGettingTooSmallOrTooBig(newFrame) self.resetFrame(to: newFrame) - startPoint = CGPointMake(pointX, pointY) + startPoint = CGPoint(x: pointX, y: pointY) } - private func preventBorderFrameFromGettingTooSmallOrTooBig(frame: CGRect) -> CGRect { - let toolbarSize = CGFloat(UIDevice.currentDevice().userInterfaceIdiom == .Pad ? 0 : 54) + private func preventBorderFrameFromGettingTooSmallOrTooBig(_ frame: CGRect) -> CGRect { + let toolbarSize = CGFloat(UIDevice.current.userInterfaceIdiom == .pad ? 0 : 54) var newFrame = frame if newFrame.size.width < 64 { newFrame.size.width = cropBorderView.frame.size.width newFrame.origin.x = cropBorderView.frame.origin.x + if lockAspectRatio { + newFrame.size.height = cropBorderView.frame.size.height + newFrame.origin.y = cropBorderView.frame.origin.y + } } + if newFrame.size.height < 64 { newFrame.size.height = cropBorderView.frame.size.height newFrame.origin.y = cropBorderView.frame.origin.y + if lockAspectRatio { + newFrame.size.width = cropBorderView.frame.size.width + newFrame.origin.x = cropBorderView.frame.origin.x + } } if newFrame.origin.x < 0 { newFrame.size.width = cropBorderView.frame.size.width + (cropBorderView.frame.origin.x - self.superview!.bounds.origin.x) newFrame.origin.x = 0 + if lockAspectRatio { + newFrame.size.height = cropBorderView.frame.size.height + + (cropBorderView.frame.origin.y - self.superview!.bounds.origin.y) + newFrame.origin.y = 0 + } } if newFrame.origin.y < 0 { newFrame.size.height = cropBorderView.frame.size.height + (cropBorderView.frame.origin.y - self.superview!.bounds.origin.y) newFrame.origin.y = 0 + if lockAspectRatio { + newFrame.size.width = cropBorderView.frame.size.width + + (cropBorderView.frame.origin.x - self.superview!.bounds.origin.x) + newFrame.origin.x = 0 + } } if newFrame.size.width + newFrame.origin.x > self.frame.size.width { - newFrame.size.width = self.frame.size.width - cropBorderView.frame.origin.x + if lockAspectRatio { + newFrame.size.width = cropBorderView.frame.size.width + newFrame.size.height = cropBorderView.frame.size.height + } + else { + newFrame.size.width = self.frame.size.width - cropBorderView.frame.origin.x + } } if newFrame.size.height + newFrame.origin.y > self.frame.size.height - toolbarSize { - newFrame.size.height = self.frame.size.height - - cropBorderView.frame.origin.y - toolbarSize + if lockAspectRatio { + newFrame.size.width = cropBorderView.frame.size.width + newFrame.size.height = cropBorderView.frame.size.height + } + else { + newFrame.size.height = self.frame.size.height - + cropBorderView.frame.origin.y - toolbarSize + } } return newFrame @@ -224,7 +267,7 @@ internal class WDResizableCropOverlayView: WDImageCropOverlayView { private func resetFrame(to frame: CGRect) { cropBorderView.frame = frame - contentView.frame = CGRectInset(frame, kBorderCorrectionValue, kBorderCorrectionValue) + contentView.frame = frame.insetBy(dx: kBorderCorrectionValue, dy: kBorderCorrectionValue) cropSize = contentView.frame.size self.setNeedsDisplay() cropBorderView.setNeedsDisplay() @@ -242,4 +285,4 @@ internal class WDResizableCropOverlayView: WDImageCropOverlayView { // 1 up, 0 middle, 0 down resizeMultiplyer.yMultiplyer = anchor.y == 0 ? 1 : 0 } -} \ No newline at end of file +} diff --git a/WDImagePicker.xcodeproj/project.pbxproj b/WDImagePicker.xcodeproj/project.pbxproj index be612e5..b3db564 100644 --- a/WDImagePicker.xcodeproj/project.pbxproj +++ b/WDImagePicker.xcodeproj/project.pbxproj @@ -189,9 +189,11 @@ TargetAttributes = { 764FD5801B8F01C100BE5F46 = { CreatedOnToolsVersion = 6.4; + LastSwiftMigration = 0800; }; 764FD5951B8F01C100BE5F46 = { CreatedOnToolsVersion = 6.4; + LastSwiftMigration = 0800; TestTargetID = 764FD5801B8F01C100BE5F46; }; }; @@ -381,6 +383,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.wudi.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -392,6 +395,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.wudi.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -408,6 +412,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.wudi.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/WDImagePicker.app/WDImagePicker"; }; name = Debug; @@ -421,6 +426,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.wudi.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/WDImagePicker.app/WDImagePicker"; }; name = Release; diff --git a/WDImagePicker/AppDelegate.swift b/WDImagePicker/AppDelegate.swift index b70318b..e9236e6 100644 --- a/WDImagePicker/AppDelegate.swift +++ b/WDImagePicker/AppDelegate.swift @@ -14,30 +14,30 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? - func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. return true } - func applicationWillResignActive(application: UIApplication) { + func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - func applicationDidEnterBackground(application: UIApplication) { + func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - func applicationWillEnterForeground(application: UIApplication) { + func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - func applicationDidBecomeActive(application: UIApplication) { + func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - func applicationWillTerminate(application: UIApplication) { + func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } diff --git a/WDImagePicker/Info.plist b/WDImagePicker/Info.plist index 40c6215..1f2717a 100644 --- a/WDImagePicker/Info.plist +++ b/WDImagePicker/Info.plist @@ -4,6 +4,8 @@ CFBundleDevelopmentRegion en + NSPhotoLibraryUsageDescription + This is a test app... CFBundleExecutable $(EXECUTABLE_NAME) CFBundleIdentifier diff --git a/WDImagePicker/ViewController.swift b/WDImagePicker/ViewController.swift index da1fa5d..248fa28 100644 --- a/WDImagePicker/ViewController.swift +++ b/WDImagePicker/ViewController.swift @@ -23,109 +23,101 @@ class ViewController: UIViewController, WDImagePickerDelegate, UIImagePickerCont super.viewDidLoad() self.customCropButton = UIButton() - self.customCropButton.frame = UIDevice.currentDevice().userInterfaceIdiom == .Pad ? - CGRectMake(20, 20, 220, 44) : - CGRectMake(20, CGRectGetMaxY(self.customCropButton.frame) + 20 , CGRectGetWidth(self.view.bounds) - 40, 44) - self.customCropButton.setTitleColor(self.view.tintColor, forState: .Normal) - self.customCropButton.setTitle("Custom Crop", forState: .Normal) - self.customCropButton.addTarget(self, action: #selector(ViewController.showPicker(_:)), forControlEvents: .TouchUpInside) + self.customCropButton.frame = UIDevice.current.userInterfaceIdiom == .pad ? + CGRect(x: 20, y: 20, width: 220, height: 44) : + CGRect(x: 20, y: self.customCropButton.frame.maxY + 20 , width: self.view.bounds.width - 40, height: 44) + self.customCropButton.setTitleColor(self.view.tintColor, for: UIControlState()) + self.customCropButton.setTitle("Custom Crop", for: UIControlState()) + self.customCropButton.addTarget(self, action: #selector(ViewController.showPicker(_:)), for: .touchUpInside) self.view.addSubview(self.customCropButton) self.normalCropButton = UIButton() - self.normalCropButton.setTitleColor(self.view.tintColor, forState: .Normal) - self.normalCropButton.setTitle("Apple's Build In Crop", forState: .Normal) - self.normalCropButton.addTarget(self, action: #selector(ViewController.showNormalPicker(_:)), forControlEvents: .TouchUpInside) + self.normalCropButton.setTitleColor(self.view.tintColor, for: UIControlState()) + self.normalCropButton.setTitle("Apple's Build In Crop", for: UIControlState()) + self.normalCropButton.addTarget(self, action: #selector(ViewController.showNormalPicker(_:)), for: .touchUpInside) self.view.addSubview(self.normalCropButton) self.resizableButton = UIButton() - self.resizableButton.setTitleColor(self.view.tintColor, forState: .Normal) - self.resizableButton.setTitle("Resizable Custom Crop", forState: .Normal) - self.resizableButton.addTarget(self, action: #selector(ViewController.showResizablePicker(_:)), forControlEvents: .TouchUpInside) + self.resizableButton.setTitleColor(self.view.tintColor, for: UIControlState()) + self.resizableButton.setTitle("Resizable Custom Crop", for: UIControlState()) + self.resizableButton.addTarget(self, action: #selector(ViewController.showResizablePicker(_:)), for: .touchUpInside) self.view.addSubview(self.resizableButton) - self.imageView = UIImageView(frame: CGRectZero) - self.imageView.contentMode = .ScaleAspectFit - self.imageView.backgroundColor = UIColor.grayColor() + self.imageView = UIImageView(frame: CGRect.zero) + self.imageView.contentMode = .scaleAspectFit + self.imageView.backgroundColor = UIColor.gray self.view.addSubview(self.imageView) } override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() - self.normalCropButton.frame = UIDevice.currentDevice().userInterfaceIdiom == .Pad ? - CGRectMake(260, 20, 220, 44) : - CGRectMake(20, CGRectGetMaxY(self.customCropButton.frame) + 20 , CGRectGetWidth(self.view.bounds) - 40, 44) + self.normalCropButton.frame = UIDevice.current.userInterfaceIdiom == .pad ? + CGRect(x: 260, y: 20, width: 220, height: 44) : + CGRect(x: 20, y: self.customCropButton.frame.maxY + 20 , width: self.view.bounds.width - 40, height: 44) - self.resizableButton.frame = UIDevice.currentDevice().userInterfaceIdiom == .Pad ? - CGRectMake(500, 20, 220, 44) : - CGRectMake(20, CGRectGetMaxY(self.normalCropButton.frame) + 20 , CGRectGetWidth(self.view.bounds) - 40, 44) + self.resizableButton.frame = UIDevice.current.userInterfaceIdiom == .pad ? + CGRect(x: 500, y: 20, width: 220, height: 44) : + CGRect(x: 20, y: self.normalCropButton.frame.maxY + 20 , width: self.view.bounds.width - 40, height: 44) - self.imageView.frame = UIDevice.currentDevice().userInterfaceIdiom == .Pad ? - CGRectMake(20, 84, CGRectGetWidth(self.view.bounds) - 40, CGRectGetHeight(self.view.bounds) - 104) : - CGRectMake(20, CGRectGetMaxY(self.resizableButton.frame) + 20, CGRectGetWidth(self.view.bounds) - 40, CGRectGetHeight(self.view.bounds) - 20 - (CGRectGetMaxY(self.resizableButton.frame) + 20)) + self.imageView.frame = UIDevice.current.userInterfaceIdiom == .pad ? + CGRect(x: 20, y: 84, width: self.view.bounds.width - 40, height: self.view.bounds.height - 104) : + CGRect(x: 20, y: self.resizableButton.frame.maxY + 20, width: self.view.bounds.width - 40, height: self.view.bounds.height - 20 - (self.resizableButton.frame.maxY + 20)) } - func showPicker(button: UIButton) { + func showPicker(_ button: UIButton) { self.imagePicker = WDImagePicker() - self.imagePicker.cropSize = CGSizeMake(280, 280) + self.imagePicker.cropSize = CGSize(width: 280, height: 280) self.imagePicker.delegate = self - if UIDevice.currentDevice().userInterfaceIdiom == .Pad { + if UIDevice.current.userInterfaceIdiom == .pad { self.popoverController = UIPopoverController(contentViewController: self.imagePicker.imagePickerController) - self.popoverController.presentPopoverFromRect(button.frame, inView: self.view, permittedArrowDirections: .Any, animated: true) + self.popoverController.present(from: button.frame, in: self.view, permittedArrowDirections: .any, animated: true) } else { - self.presentViewController(self.imagePicker.imagePickerController, animated: true, completion: nil) + self.present(self.imagePicker.imagePickerController, animated: true, completion: nil) } } - func showNormalPicker(button: UIButton) { + func showNormalPicker(_ button: UIButton) { self.imagePickerController = UIImagePickerController() - self.imagePickerController.sourceType = .PhotoLibrary + self.imagePickerController.sourceType = .photoLibrary self.imagePickerController.delegate = self self.imagePickerController.allowsEditing = true - if UIDevice.currentDevice().userInterfaceIdiom == .Pad { + if UIDevice.current.userInterfaceIdiom == .pad { self.popoverController = UIPopoverController(contentViewController: self.imagePickerController) - self.popoverController.presentPopoverFromRect(button.frame, inView: self.view, permittedArrowDirections: .Any, animated: true) + self.popoverController.present(from: button.frame, in: self.view, permittedArrowDirections: .any, animated: true) } else { - self.presentViewController(self.imagePickerController, animated: true, completion: nil) + self.present(self.imagePickerController, animated: true, completion: nil) } } - func showResizablePicker(button: UIButton) { + func showResizablePicker(_ button: UIButton) { self.imagePicker = WDImagePicker() - self.imagePicker.cropSize = CGSizeMake(280, 280) + self.imagePicker.cropSize = CGSize(width: 280, height: 280) self.imagePicker.delegate = self self.imagePicker.resizableCropArea = true - if UIDevice.currentDevice().userInterfaceIdiom == .Pad { + if UIDevice.current.userInterfaceIdiom == .pad { self.popoverController = UIPopoverController(contentViewController: self.imagePicker.imagePickerController) - self.popoverController.presentPopoverFromRect(button.frame, inView: self.view, permittedArrowDirections: .Any, animated: true) + self.popoverController.present(from: button.frame, in: self.view, permittedArrowDirections: .any, animated: true) } else { - self.presentViewController(self.imagePicker.imagePickerController, animated: true, completion: nil) + self.present(self.imagePicker.imagePickerController, animated: true, completion: nil) } } - func imagePicker(imagePicker: WDImagePicker, pickedImage: UIImage) { + func imagePicker(_ imagePicker: WDImagePicker, pickedImage: UIImage) { self.imageView.image = pickedImage self.hideImagePicker() } - + + var prefersPickedImageRectangle = false + func hideImagePicker() { - if UIDevice.currentDevice().userInterfaceIdiom == .Pad { - self.popoverController.dismissPopoverAnimated(true) - } else { - self.imagePicker.imagePickerController.dismissViewControllerAnimated(true, completion: nil) - } - } - - func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) { - self.imageView.image = image - - if UIDevice.currentDevice().userInterfaceIdiom == .Pad { - self.popoverController.dismissPopoverAnimated(true) + if UIDevice.current.userInterfaceIdiom == .pad { + self.popoverController.dismiss(animated: true) } else { - picker.dismissViewControllerAnimated(true, completion: nil) + self.imagePicker.imagePickerController.dismiss(animated: true, completion: nil) } } } diff --git a/WDImagePickerTests/WDImagePickerTests.swift b/WDImagePickerTests/WDImagePickerTests.swift index 61ceed1..c6c903b 100644 --- a/WDImagePickerTests/WDImagePickerTests.swift +++ b/WDImagePickerTests/WDImagePickerTests.swift @@ -28,7 +28,7 @@ class WDImagePickerTests: XCTestCase { func testPerformanceExample() { // This is an example of a performance test case. - self.measureBlock() { + self.measure() { // Put the code you want to measure the time of here. } }