diff --git a/Classes/WDCropBorderView.swift b/Classes/WDCropBorderView.swift index ef985c1..5de9901 100644 --- a/Classes/WDCropBorderView.swift +++ b/Classes/WDCropBorderView.swift @@ -9,38 +9,37 @@ import UIKit internal class WDCropBorderView: UIView { - private let kNumberOfBorderHandles: CGFloat = 8 - private let kHandleDiameter: CGFloat = 24 + fileprivate let kNumberOfBorderHandles: CGFloat = 8 + fileprivate let kHandleDiameter: CGFloat = 24 override init(frame: CGRect) { super.init(frame: frame) - self.backgroundColor = UIColor.clearColor() + self.backgroundColor = UIColor.clear } required init?(coder aDecoder: NSCoder) { 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) } } - private func calculateAllNeededHandleRects() -> [CGRect] { + fileprivate func calculateAllNeededHandleRects() -> [CGRect] { let width = self.frame.width let height = self.frame.height @@ -54,16 +53,16 @@ internal class WDCropBorderView: UIView { 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) + let topLeft = CGRect(x: leftColX, y: topRowY, width: kHandleDiameter, height: kHandleDiameter) + let topCenter = CGRect(x: centerColX, y: topRowY, width: kHandleDiameter, height: kHandleDiameter) + let topRight = CGRect(x: rightColX, y: topRowY, width: kHandleDiameter, height: kHandleDiameter) + let middleRight = CGRect(x: rightColX, y: middleRowY, width: kHandleDiameter, height: kHandleDiameter) + let bottomRight = CGRect(x: rightColX, y: bottomRowY, width: kHandleDiameter, height: kHandleDiameter) + let bottomCenter = CGRect(x: centerColX, y: bottomRowY, width: kHandleDiameter, height: kHandleDiameter) + let bottomLeft = CGRect(x: leftColX, y: bottomRowY, width: kHandleDiameter, height: kHandleDiameter) + let middleLeft = CGRect(x: leftColX, y: middleRowY, width: kHandleDiameter, height: kHandleDiameter) return [topLeft, topCenter, topRight, middleRight, bottomRight, bottomCenter, bottomLeft, middleLeft] } -} \ 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..a05f2c9 100644 --- a/Classes/WDImageCropView.swift +++ b/Classes/WDImageCropView.swift @@ -10,10 +10,10 @@ import UIKit import QuartzCore private class ScrollView: UIScrollView { - private override func layoutSubviews() { + fileprivate 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 @@ -39,18 +39,18 @@ private class ScrollView: UIScrollView { internal class WDImageCropView: UIView, UIScrollViewDelegate { var resizableCropArea = false - private var scrollView: UIScrollView! - private var imageView: UIImageView! - private var cropOverlayView: WDImageCropOverlayView! - 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) + fileprivate var scrollView: UIScrollView! + fileprivate var imageView: UIImageView! + fileprivate var cropOverlayView: WDImageCropOverlayView! + fileprivate var xOffset: CGFloat! + fileprivate var yOffset: CGFloat! + + fileprivate 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 +72,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)) } else { self.cropOverlayView = WDImageCropOverlayView(frame: self.bounds) } @@ -85,24 +85,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 +111,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 +145,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,13 +167,13 @@ 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 } @@ -184,17 +184,17 @@ internal class WDImageCropView: UIView, UIScrollViewDelegate { // 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, + let imageRef = imageToCrop!.cgImage?.cropping(to: visibleRect) + let result = UIImage(cgImage: imageRef!, scale: imageToCrop!.scale, orientation: imageToCrop!.imageOrientation) return result } - private func calcVisibleRectForResizeableCropArea() -> CGRect { + fileprivate func calcVisibleRectForResizeableCropArea() -> CGRect { let resizableView = cropOverlayView as! WDResizableCropOverlayView // first of all, get the size scale by taking a look at the real image dimensions. Here it @@ -204,14 +204,14 @@ 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 } - private func calcVisibleRectForCropArea() -> CGRect { + fileprivate func calcVisibleRectForCropArea() -> CGRect { // scaled width/height in regards of real width to crop width let scaleWidth = imageToCrop!.size.width / cropSize.width let scaleHeight = imageToCrop!.size.height / cropSize.height @@ -230,30 +230,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 { + fileprivate 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..023542c 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, didFinishWithCroppedImage croppedImage: UIImage) } internal class WDImageCropViewController: UIViewController { @@ -19,12 +19,12 @@ internal class WDImageCropViewController: UIViewController { var cropSize: CGSize! var resizableCropArea = false - private var croppedImage: UIImage! + fileprivate var croppedImage: UIImage! - private var imageCropView: WDImageCropView! - private var toolbar: UIToolbar! - private var useButton: UIButton! - private var cancelButton: UIButton! + fileprivate var imageCropView: WDImageCropView! + fileprivate var toolbar: UIToolbar! + fileprivate var useButton: UIButton! + fileprivate var cancelButton: UIButton! override func viewDidLoad() { super.viewDidLoad() @@ -37,10 +37,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,27 +48,27 @@ 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.navigationController?.popViewController(animated: true) } - func actionUse(sender: AnyObject) { + func actionUse(_ sender: AnyObject) { croppedImage = self.imageCropView.croppedImage() self.delegate?.imageCropController(self, didFinishWithCroppedImage: croppedImage) } - private func setupNavigationBar() { - self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Cancel, + fileprivate func setupNavigationBar() { + 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)) } - private func setupCropView() { + fileprivate func setupCropView() { self.imageCropView = WDImageCropView(frame: self.view.bounds) self.imageCropView.imageToCrop = sourceImage self.imageCropView.resizableCropArea = self.resizableCropArea @@ -76,67 +76,67 @@ internal class WDImageCropViewController: UIViewController { self.view.addSubview(self.imageCropView) } - private func setupCancelButton() { + fileprivate 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() { + fileprivate 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 { + fileprivate 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(colorSpace: colorSpace, colorComponents: 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 + fileprivate func setupToolbar() { + 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..08717e4 100644 --- a/Classes/WDImagePicker.swift +++ b/Classes/WDImagePicker.swift @@ -9,35 +9,35 @@ 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) + @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 +@objc open class WDImagePicker: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate, WDImageCropControllerDelegate { + open var delegate: WDImagePickerDelegate? + open var cropSize: CGSize! + open var resizableCropArea = false - private var _imagePickerController: UIImagePickerController! + fileprivate var _imagePickerController: UIImagePickerController! - public var imagePickerController: UIImagePickerController { + open var imagePickerController: UIImagePickerController { return _imagePickerController } 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) + fileprivate func hideController() { + self._imagePickerController.dismiss(animated: true, completion: nil) } - public func imagePickerControllerDidCancel(picker: UIImagePickerController) { + open func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { if self.delegate?.imagePickerDidCancel != nil { self.delegate?.imagePickerDidCancel!(self) } else { @@ -45,7 +45,7 @@ import UIKit } } - public func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { + open func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { let cropController = WDImageCropViewController() cropController.sourceImage = info[UIImagePickerControllerOriginalImage] as! UIImage cropController.resizableCropArea = self.resizableCropArea @@ -54,7 +54,7 @@ import UIKit picker.pushViewController(cropController, animated: true) } - func imageCropController(imageCropController: WDImageCropViewController, didFinishWithCroppedImage croppedImage: UIImage) { + func imageCropController(_ imageCropController: WDImageCropViewController, didFinishWithCroppedImage croppedImage: UIImage) { self.delegate?.imagePicker?(self, pickedImage: croppedImage) } } diff --git a/Classes/WDResizableCropOverlayView.swift b/Classes/WDResizableCropOverlayView.swift index b877235..51b1096 100644 --- a/Classes/WDResizableCropOverlayView.swift +++ b/Classes/WDResizableCropOverlayView.swift @@ -16,16 +16,16 @@ private struct WDResizableViewBorderMultiplyer { } internal class WDResizableCropOverlayView: WDImageCropOverlayView { - private let kBorderCorrectionValue: CGFloat = 12 + fileprivate let kBorderCorrectionValue: CGFloat = 12 var contentView: UIView! var cropBorderView: WDCropBorderView! - private var initialContentSize = CGSize(width: 0, height: 0) - private var resizingEnabled: Bool! - private var anchor: CGPoint! - private var startPoint: CGPoint! - private var resizeMultiplyer = WDResizableViewBorderMultiplyer() + fileprivate var initialContentSize = CGSize(width: 0, height: 0) + fileprivate var resizingEnabled: Bool! + fileprivate var anchor: CGPoint! + fileprivate var startPoint: CGPoint! + fileprivate var resizeMultiplyer = WDResizableViewBorderMultiplyer() override var frame: CGRect { get { @@ -34,21 +34,21 @@ 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) } } @@ -67,58 +67,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) + fileprivate func addContentViews() { + 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)) self.addSubview(cropBorderView) } - private func calculateAnchorBorder(anchorPoint: CGPoint) -> CGPoint { + fileprivate func calculateAnchorBorder(_ anchorPoint: CGPoint) -> CGPoint { let allHandles = getAllCurrentHandlePositions() var closest: CGFloat = 3000 var anchor: CGPoint! @@ -136,7 +136,7 @@ internal class WDResizableCropOverlayView: WDImageCropOverlayView { return anchor } - private func getAllCurrentHandlePositions() -> [CGPoint] { + fileprivate func getAllCurrentHandlePositions() -> [CGPoint] { let leftX: CGFloat = 0 let rightX = cropBorderView.bounds.size.width let centerX = leftX + (rightX - leftX) / 2 @@ -146,20 +146,20 @@ internal class WDResizableCropOverlayView: WDImageCropOverlayView { 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) + let topLeft = CGPoint(x: leftX, y: topY) + let topCenter = CGPoint(x: centerX, y: topY) + let topRight = CGPoint(x: rightX, y: topY) + let middleRight = CGPoint(x: rightX, y: middleY) + let bottomRight = CGPoint(x: rightX, y: bottomY) + let bottomCenter = CGPoint(x: centerX, y: bottomY) + let bottomLeft = CGPoint(x: leftX, y: bottomY) + let middleLeft = CGPoint(x: leftX, y: middleY) return [topLeft, topCenter, topRight, middleRight, bottomRight, bottomCenter, bottomLeft, middleLeft] } - private func resizeWithTouchPoint(point: CGPoint) { + fileprivate func resizeWithTouchPoint(_ point: CGPoint) { // This is the place where all the magic happends // prevent goint offscreen... let border = kBorderCorrectionValue * 2 @@ -175,18 +175,18 @@ internal class WDResizableCropOverlayView: WDImageCropOverlayView { 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) + fileprivate func preventBorderFrameFromGettingTooSmallOrTooBig(_ frame: CGRect) -> CGRect { + let toolbarSize = CGFloat(UIDevice.current.userInterfaceIdiom == .pad ? 0 : 54) var newFrame = frame if newFrame.size.width < 64 { @@ -222,15 +222,15 @@ internal class WDResizableCropOverlayView: WDImageCropOverlayView { return newFrame } - private func resetFrame(to frame: CGRect) { + fileprivate 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() } - private func fillMultiplyer() { + fileprivate func fillMultiplyer() { // -1 left, 0 middle, 1 right resizeMultiplyer.heightMultiplyer = anchor.y == 0 ? -1 : anchor.y == cropBorderView.bounds.size.height ? 1 : 0 @@ -242,4 +242,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..3e66030 100644 --- a/WDImagePicker.xcodeproj/project.pbxproj +++ b/WDImagePicker.xcodeproj/project.pbxproj @@ -184,14 +184,16 @@ attributes = { LastSwiftMigration = 0730; LastSwiftUpdateCheck = 0730; - LastUpgradeCheck = 0730; + LastUpgradeCheck = 0810; ORGANIZATIONNAME = "Wu Di"; TargetAttributes = { 764FD5801B8F01C100BE5F46 = { CreatedOnToolsVersion = 6.4; + LastSwiftMigration = 0810; }; 764FD5951B8F01C100BE5F46 = { CreatedOnToolsVersion = 6.4; + LastSwiftMigration = 0810; TestTargetID = 764FD5801B8F01C100BE5F46; }; }; @@ -302,8 +304,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -348,8 +352,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -368,6 +374,7 @@ IPHONEOS_DEPLOYMENT_TARGET = 8.4; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; TARGETED_DEVICE_FAMILY = "1,2"; VALIDATE_PRODUCT = YES; }; @@ -381,6 +388,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 +400,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 +417,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 +431,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..8227226 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: [UIApplicationLaunchOptionsKey: Any]?) -> 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/Base.lproj/LaunchScreen.xib b/WDImagePicker/Base.lproj/LaunchScreen.xib index 5cfcee0..a981a79 100644 --- a/WDImagePicker/Base.lproj/LaunchScreen.xib +++ b/WDImagePicker/Base.lproj/LaunchScreen.xib @@ -1,8 +1,13 @@ - - + + + + + - + + + @@ -24,7 +29,7 @@ - + diff --git a/WDImagePicker/Base.lproj/Main.storyboard b/WDImagePicker/Base.lproj/Main.storyboard index 3a2a49b..8cdcdec 100644 --- a/WDImagePicker/Base.lproj/Main.storyboard +++ b/WDImagePicker/Base.lproj/Main.storyboard @@ -1,21 +1,26 @@ - - + + + + + - + + + - + - + - + diff --git a/WDImagePicker/ViewController.swift b/WDImagePicker/ViewController.swift index da1fa5d..1e32fd2 100644 --- a/WDImagePicker/ViewController.swift +++ b/WDImagePicker/ViewController.swift @@ -10,122 +10,122 @@ import UIKit class ViewController: UIViewController, WDImagePickerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate { - private var imagePicker: WDImagePicker! - private var popoverController: UIPopoverController! - private var imagePickerController: UIImagePickerController! + fileprivate var imagePicker: WDImagePicker! + fileprivate var popoverController: UIPopoverController! + fileprivate var imagePickerController: UIImagePickerController! - private var customCropButton: UIButton! - private var normalCropButton: UIButton! - private var imageView: UIImageView! - private var resizableButton: UIButton! + fileprivate var customCropButton: UIButton! + fileprivate var normalCropButton: UIButton! + fileprivate var imageView: UIImageView! + fileprivate var resizableButton: UIButton! override func viewDidLoad() { 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() } func hideImagePicker() { - if UIDevice.currentDevice().userInterfaceIdiom == .Pad { - self.popoverController.dismissPopoverAnimated(true) + if UIDevice.current.userInterfaceIdiom == .pad { + self.popoverController.dismiss(animated: true) } else { - self.imagePicker.imagePickerController.dismissViewControllerAnimated(true, completion: nil) + self.imagePicker.imagePickerController.dismiss(animated: true, completion: nil) } } - func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) { + func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [AnyHashable: Any]!) { 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) + picker.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. } }