diff --git a/Classes/WDCropBorderView.swift b/Classes/WDCropBorderView.swift index ef985c1..2ff3d3a 100644 --- a/Classes/WDCropBorderView.swift +++ b/Classes/WDCropBorderView.swift @@ -27,16 +27,16 @@ internal class WDCropBorderView: UIView { override func drawRect(rect: CGRect) { let context = UIGraphicsGetCurrentContext() - CGContextSetStrokeColorWithColor(context, + CGContextSetStrokeColorWithColor(context!, UIColor(red: 1, green: 1, blue: 1, alpha: 0.5).CGColor) - CGContextSetLineWidth(context, 1.5) - CGContextAddRect(context, CGRectMake(kHandleDiameter / 2, kHandleDiameter / 2, + CGContextSetLineWidth(context!, 1.5) + CGContextAddRect(context!, CGRectMake(kHandleDiameter / 2, kHandleDiameter / 2, rect.size.width - kHandleDiameter, rect.size.height - kHandleDiameter)) - CGContextStrokePath(context) + CGContextStrokePath(context!) - CGContextSetRGBFillColor(context, 1, 1, 1, 0.95) + CGContextSetRGBFillColor(context!, 1, 1, 1, 0.95) for handleRect in calculateAllNeededHandleRects() { - CGContextFillEllipseInRect(context, handleRect) + CGContextFillEllipseInRect(context!, handleRect) } } @@ -66,4 +66,4 @@ internal class WDCropBorderView: UIView { return [topLeft, topCenter, topRight, middleRight, bottomRight, bottomCenter, bottomLeft, middleLeft] } -} \ No newline at end of file +} diff --git a/Classes/WDImageCropView.swift b/Classes/WDImageCropView.swift index 7158dfe..c248518 100644 --- a/Classes/WDImageCropView.swift +++ b/Classes/WDImageCropView.swift @@ -187,7 +187,7 @@ internal class WDImageCropView: UIView, UIScrollViewDelegate { visibleRect = CGRectApplyAffineTransform(visibleRect, rectTransform); // finally crop image - let imageRef = CGImageCreateWithImageInRect(imageToCrop!.CGImage, visibleRect) + let imageRef = CGImageCreateWithImageInRect(imageToCrop!.CGImage!, visibleRect) let result = UIImage(CGImage: imageRef!, scale: imageToCrop!.scale, orientation: imageToCrop!.imageOrientation) @@ -256,4 +256,4 @@ internal class WDImageCropView: UIView, UIScrollViewDelegate { return CGAffineTransformScale(rectTransform, image.scale, image.scale) } -} \ No newline at end of file +} diff --git a/Classes/WDImageCropViewController.swift b/Classes/WDImageCropViewController.swift index b244791..602a1cc 100644 --- a/Classes/WDImageCropViewController.swift +++ b/Classes/WDImageCropViewController.swift @@ -107,13 +107,13 @@ internal class WDImageCropViewController: UIViewController { let colorSpace = CGColorSpaceCreateDeviceRGB() let gradient = CGGradientCreateWithColorComponents(colorSpace, components, nil, 2) - CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(0, 54), []) + CGContextDrawLinearGradient(context!, gradient!, CGPointMake(0, 0), CGPointMake(0, 54), []) let viewImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() - return viewImage + return viewImage! } private func setupToolbar() { diff --git a/Classes/WDImagePicker.swift b/Classes/WDImagePicker.swift index 70129de..526b628 100644 --- a/Classes/WDImagePicker.swift +++ b/Classes/WDImagePicker.swift @@ -33,6 +33,16 @@ import UIKit _imagePickerController.sourceType = .PhotoLibrary } + //This is the custom init method to define the source type, + //Use .Camera for camera and .PhotoLibrary for photolibrary + public init(withSourceType sourceType : UIImagePickerControllerSourceType){ + super.init() + self.cropSize = CGSizeMake(320, 320) + _imagePickerController = UIImagePickerController() + _imagePickerController.delegate = self + _imagePickerController.sourceType = sourceType + } + private func hideController() { self._imagePickerController.dismissViewControllerAnimated(true, completion: nil) } diff --git a/README.md b/README.md index 66e3f9d..f340443 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,8 @@ and run `pod install` ### Sample Code ```swift -self.imagePicker = WDImagePicker() +self.imagePicker = WDImagePicker() //default source type is PhotoLibrary +self.imagePicker = WDImagePicker.init(withSourceType: .Camera) //use this for camera self.imagePicker.cropSize = CGSizeMake(280, 90) self.imagePicker.delegate = self diff --git a/WDImagePicker.xcodeproj/project.pbxproj b/WDImagePicker.xcodeproj/project.pbxproj index be612e5..1887b87 100644 --- a/WDImagePicker.xcodeproj/project.pbxproj +++ b/WDImagePicker.xcodeproj/project.pbxproj @@ -313,6 +313,7 @@ ENABLE_TESTABILITY = YES; GCC_C_LANGUAGE_STANDARD = gnu99; GCC_DYNAMIC_NO_PIC = NO; + GCC_GENERATE_TEST_COVERAGE_FILES = YES; GCC_NO_COMMON_BLOCKS = YES; GCC_OPTIMIZATION_LEVEL = 0; GCC_PREPROCESSOR_DEFINITIONS = ( @@ -331,6 +332,7 @@ ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 2.3; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Debug; @@ -358,6 +360,7 @@ ENABLE_NS_ASSERTIONS = NO; ENABLE_STRICT_OBJC_MSGSEND = YES; GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_GENERATE_TEST_COVERAGE_FILES = YES; GCC_NO_COMMON_BLOCKS = YES; GCC_WARN_64_TO_32_BIT_CONVERSION = YES; GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; @@ -368,6 +371,7 @@ IPHONEOS_DEPLOYMENT_TARGET = 8.4; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; + SWIFT_VERSION = 2.3; TARGETED_DEVICE_FAMILY = "1,2"; VALIDATE_PRODUCT = YES; }; @@ -377,10 +381,13 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + DEVELOPMENT_TEAM = ""; + GCC_GENERATE_TEST_COVERAGE_FILES = YES; INFOPLIST_FILE = WDImagePicker/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.wudi.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 2.3; }; name = Debug; }; @@ -388,10 +395,13 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + DEVELOPMENT_TEAM = ""; + GCC_GENERATE_TEST_COVERAGE_FILES = YES; INFOPLIST_FILE = WDImagePicker/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.wudi.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 2.3; }; name = Release; }; @@ -400,6 +410,7 @@ buildSettings = { BUNDLE_LOADER = "$(TEST_HOST)"; FRAMEWORK_SEARCH_PATHS = ""; + GCC_GENERATE_TEST_COVERAGE_FILES = YES; GCC_PREPROCESSOR_DEFINITIONS = ( "DEBUG=1", "$(inherited)", @@ -408,6 +419,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 = 2.3; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/WDImagePicker.app/WDImagePicker"; }; name = Debug; @@ -417,10 +429,12 @@ buildSettings = { BUNDLE_LOADER = "$(TEST_HOST)"; FRAMEWORK_SEARCH_PATHS = ""; + GCC_GENERATE_TEST_COVERAGE_FILES = YES; INFOPLIST_FILE = WDImagePickerTests/Info.plist; 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 = 2.3; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/WDImagePicker.app/WDImagePicker"; }; name = Release; diff --git a/WDImagePicker/Info.plist b/WDImagePicker/Info.plist index 40c6215..9e4e474 100644 --- a/WDImagePicker/Info.plist +++ b/WDImagePicker/Info.plist @@ -2,6 +2,10 @@ + NSCameraUsageDescription + Please allow access to Camera + NSPhotoLibraryUsageDescription + Please allow access to Photo Library CFBundleDevelopmentRegion en CFBundleExecutable diff --git a/WDImagePicker/ViewController.swift b/WDImagePicker/ViewController.swift index da1fa5d..761a777 100644 --- a/WDImagePicker/ViewController.swift +++ b/WDImagePicker/ViewController.swift @@ -66,16 +66,27 @@ class ViewController: UIViewController, WDImagePickerDelegate, UIImagePickerCont } func showPicker(button: UIButton) { - self.imagePicker = WDImagePicker() - self.imagePicker.cropSize = CGSizeMake(280, 280) - self.imagePicker.delegate = self - + let actionSheet = UIAlertController.init(title: "Please choose a source type", message: nil, preferredStyle: .ActionSheet) + actionSheet.addAction(UIAlertAction.init(title: "Take Photo", style: UIAlertActionStyle.Default, handler: { (action) in + self.showCamera(button, isCropAreaResizable: false) + })) + actionSheet.addAction(UIAlertAction.init(title: "Choose Photo", style: UIAlertActionStyle.Default, handler: { (action) in + self.showPhotoGallery(button, isCropAreaResizable: false) + })) + + actionSheet.addAction(UIAlertAction.init(title: "Cancel", style: UIAlertActionStyle.Default, handler: { (action) in + self.dismissViewControllerAnimated(true, completion: nil) + })) + if UIDevice.currentDevice().userInterfaceIdiom == .Pad { - self.popoverController = UIPopoverController(contentViewController: self.imagePicker.imagePickerController) + self.popoverController = UIPopoverController(contentViewController: actionSheet) self.popoverController.presentPopoverFromRect(button.frame, inView: self.view, permittedArrowDirections: .Any, animated: true) } else { - self.presentViewController(self.imagePicker.imagePickerController, animated: true, completion: nil) + self.presentViewController(actionSheet, animated: true, completion: nil) } + +// self.presentViewController(actionSheet, animated: true, completion: nil) + } func showNormalPicker(button: UIButton) { @@ -93,16 +104,23 @@ class ViewController: UIViewController, WDImagePickerDelegate, UIImagePickerCont } func showResizablePicker(button: UIButton) { - self.imagePicker = WDImagePicker() - self.imagePicker.cropSize = CGSizeMake(280, 280) - self.imagePicker.delegate = self - self.imagePicker.resizableCropArea = true - + let actionSheet = UIAlertController.init(title: "Please choose a source type", message: nil, preferredStyle: .ActionSheet) + actionSheet.addAction(UIAlertAction.init(title: "Take Photo", style: UIAlertActionStyle.Default, handler: { (action) in + self.showCamera(button, isCropAreaResizable: true) + })) + actionSheet.addAction(UIAlertAction.init(title: "Choose Photo", style: UIAlertActionStyle.Default, handler: { (action) in + self.showPhotoGallery(button, isCropAreaResizable: true) + })) + + actionSheet.addAction(UIAlertAction.init(title: "Cancel", style: UIAlertActionStyle.Default, handler: { (action) in + self.dismissViewControllerAnimated(true, completion: nil) + })) + if UIDevice.currentDevice().userInterfaceIdiom == .Pad { - self.popoverController = UIPopoverController(contentViewController: self.imagePicker.imagePickerController) + self.popoverController = UIPopoverController(contentViewController: actionSheet) self.popoverController.presentPopoverFromRect(button.frame, inView: self.view, permittedArrowDirections: .Any, animated: true) } else { - self.presentViewController(self.imagePicker.imagePickerController, animated: true, completion: nil) + self.presentViewController(actionSheet, animated: true, completion: nil) } } @@ -128,5 +146,37 @@ class ViewController: UIViewController, WDImagePickerDelegate, UIImagePickerCont picker.dismissViewControllerAnimated(true, completion: nil) } } + + func showPhotoGallery(button : UIButton, isCropAreaResizable : Bool){ + + self.imagePicker = WDImagePicker.init(withSourceType: .PhotoLibrary) + self.imagePicker.cropSize = CGSizeMake(280, 280) + self.imagePicker.delegate = self + self.imagePicker.resizableCropArea = isCropAreaResizable + + if UIDevice.currentDevice().userInterfaceIdiom == .Pad { + self.popoverController = UIPopoverController(contentViewController: self.imagePicker.imagePickerController) + self.popoverController.presentPopoverFromRect(button.frame, inView: self.view, permittedArrowDirections: .Any, animated: true) + } else { + self.presentViewController(self.imagePicker.imagePickerController, animated: true, completion: nil) + } + + } + + func showCamera(button : UIButton, isCropAreaResizable : Bool){ + self.imagePicker = WDImagePicker.init(withSourceType: .Camera) + self.imagePicker.cropSize = CGSizeMake(280, 280) + self.imagePicker.delegate = self + self.imagePicker.resizableCropArea = isCropAreaResizable + + if UIDevice.currentDevice().userInterfaceIdiom == .Pad { + self.popoverController = UIPopoverController(contentViewController: self.imagePicker.imagePickerController) + self.popoverController.presentPopoverFromRect(button.frame, inView: self.view, permittedArrowDirections: .Any, animated: true) + } else { + self.presentViewController(self.imagePicker.imagePickerController, animated: true, completion: nil) + } + } + + }