Skip to content
This repository was archived by the owner on Apr 6, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Classes/WDCropBorderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -66,4 +66,4 @@ internal class WDCropBorderView: UIView {
return [topLeft, topCenter, topRight, middleRight, bottomRight, bottomCenter, bottomLeft,
middleLeft]
}
}
}
4 changes: 2 additions & 2 deletions Classes/WDImageCropView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -256,4 +256,4 @@ internal class WDImageCropView: UIView, UIScrollViewDelegate {

return CGAffineTransformScale(rectTransform, image.scale, image.scale)
}
}
}
4 changes: 2 additions & 2 deletions Classes/WDImageCropViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
10 changes: 10 additions & 0 deletions Classes/WDImagePicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions WDImagePicker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
};
Expand All @@ -377,21 +381,27 @@
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;
};
764FD5A21B8F01C100BE5F46 /* Release */ = {
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;
};
Expand All @@ -400,6 +410,7 @@
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
FRAMEWORK_SEARCH_PATHS = "";
GCC_GENERATE_TEST_COVERAGE_FILES = YES;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
Expand All @@ -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;
Expand All @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions WDImagePicker/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSCameraUsageDescription</key>
<string>Please allow access to Camera</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Please allow access to Photo Library</string>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
Expand Down
76 changes: 63 additions & 13 deletions WDImagePicker/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
}

Expand All @@ -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)
}
}


}