Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring the ImagePicker. #72

Merged
merged 10 commits into from
Dec 11, 2015
Merged
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
11 changes: 2 additions & 9 deletions Demo/ImagePickerDemo/ImagePickerDemo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,8 @@ import ImagePicker
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

lazy var imagePickerController: ImagePickerController = {
let imagePickerController = ImagePickerController()
return imagePickerController
}()

lazy var window: UIWindow? = {
let window = UIWindow(frame: UIScreen.mainScreen().bounds)
return window
}()
lazy var imagePickerController: ImagePickerController = ImagePickerController()
lazy var window: UIWindow? = UIWindow(frame: UIScreen.mainScreen().bounds)

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window?.rootViewController = imagePickerController
Expand Down
Binary file removed Images/AUTO.png
Binary file not shown.
Binary file removed Images/AUTO@2x.png
Binary file not shown.
Binary file removed Images/OFF.png
Binary file not shown.
Binary file removed Images/OFF@2x.png
Binary file not shown.
Binary file removed Images/ON.png
Binary file not shown.
Binary file removed Images/ON@2x.png
Binary file not shown.
Binary file removed Images/cameraIcon.png
Binary file not shown.
Binary file removed Images/cameraIcon@2x.png
Binary file not shown.
Binary file removed Images/focusIcon.png
Binary file not shown.
Binary file removed Images/focusIcon@2x.png
Binary file not shown.
Binary file removed Images/selectedImageGallery.png
Binary file not shown.
Binary file removed Images/selectedImageGallery@2x.png
Binary file not shown.
4 changes: 2 additions & 2 deletions Source/BottomView/ImageStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public class ImageStack {
NSNotificationCenter.defaultCenter().postNotificationName(Notifications.imageDidDrop, object: self, userInfo: ["image" : asset])
}

public func resetAssets(assets: [PHAsset]) {
self.assets = assets
public func resetAssets(assetsArray: [PHAsset]) {
assets = assetsArray
NSNotificationCenter.defaultCenter().postNotificationName(Notifications.stackDidReload, object: self, userInfo: nil)
}

Expand Down
38 changes: 20 additions & 18 deletions Source/CameraView/CameraView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,7 @@ class CameraView: UIViewController {

captureDevice = capturedDevices?.firstObject as? AVCaptureDevice

if captureDevice != nil {
beginSession()
}
if captureDevice != nil { beginSession() }
}

// MARK: - Actions
Expand Down Expand Up @@ -190,7 +188,12 @@ class CameraView: UIViewController {
? AVCaptureSessionPreset1280x720
: AVCaptureSessionPreset640x480

try! self.captureSession.addInput(AVCaptureDeviceInput(device: self.captureDevice))
do { try
self.captureSession.addInput(AVCaptureDeviceInput(device: self.captureDevice))
} catch {
print("There was an error capturing your device.")
}

self.captureSession.commitConfiguration()
UIView.animateWithDuration(0.7, animations: { [unowned self] in
self.containerView.alpha = 0
Expand All @@ -199,21 +202,20 @@ class CameraView: UIViewController {
}

func flashCamera(title: String) {
guard let _ = captureDevice?.hasFlash else { return }

do {
try captureDevice?.lockForConfiguration()
} catch _ { }

switch title {
case "ON":
captureDevice?.flashMode = .On
case "OFF":
captureDevice?.flashMode = .Off
default:
captureDevice?.flashMode = .Auto

if (captureDevice?.hasFlash != nil) {
do {
try captureDevice?.lockForConfiguration()
} catch _ {
}
switch title {
case "ON":
captureDevice?.flashMode = .On
case "OFF":
captureDevice?.flashMode = .Off
default:
captureDevice?.flashMode = .Auto

}
}
}

Expand Down
21 changes: 12 additions & 9 deletions Source/ImagePickerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,10 @@ public class ImagePickerController: UIViewController {

public weak var delegate: ImagePickerDelegate?
public var stack = ImageStack()
let totalHeight = UIScreen.mainScreen().bounds.size.height
let totalWidth = UIScreen.mainScreen().bounds.size.width
var initialFrame: CGRect!
var initialContentOffset: CGPoint!
var numberOfCells: Int!
let totalSize = UIScreen.mainScreen().bounds.size
var initialFrame: CGRect?
var initialContentOffset: CGPoint?
var numberOfCells: Int?
var statusBarHidden = true

public var doneButtonTitle: String? {
Expand Down Expand Up @@ -106,8 +105,8 @@ public class ImagePickerController: UIViewController {
let galleryHeight: CGFloat = UIScreen.mainScreen().nativeBounds.height == 960
? ImageGalleryView.Dimensions.galleryBarHeight : GestureConstants.minimumHeight

galleryView.frame = CGRectMake(0, totalHeight - bottomContainer.frame.height - galleryHeight,
totalWidth, galleryHeight)
galleryView.frame = CGRectMake(0, totalSize.height - bottomContainer.frame.height - galleryHeight,
totalSize.width, galleryHeight)
galleryView.updateFrames()
galleryView.checkStatus()

Expand Down Expand Up @@ -192,7 +191,7 @@ public class ImagePickerController: UIViewController {
}

func updateGalleryViewFrames(constant: CGFloat) {
galleryView.frame.origin.y = totalHeight - bottomContainer.frame.height - constant
galleryView.frame.origin.y = totalSize.height - bottomContainer.frame.height - constant
galleryView.frame.size.height = constant
}

Expand Down Expand Up @@ -306,7 +305,7 @@ extension ImagePickerController: ImageGalleryPanGestureDelegate {

initialFrame = galleryView.frame
initialContentOffset = galleryView.collectionView.contentOffset
numberOfCells = Int(initialContentOffset.x / collectionSize.width)
if let contentOffset = initialContentOffset { numberOfCells = Int(contentOffset.x / collectionSize.width) }
}

func panGestureRecognizerHandler(gesture: UIPanGestureRecognizer) {
Expand All @@ -323,6 +322,8 @@ extension ImagePickerController: ImageGalleryPanGestureDelegate {
}

func panGestureDidChange(translation: CGPoint) {
guard let initialFrame = initialFrame else { return }

let galleryHeight = initialFrame.height - translation.y

if galleryHeight >= GestureConstants.maximumHeight { return }
Expand Down Expand Up @@ -350,6 +351,8 @@ extension ImagePickerController: ImageGalleryPanGestureDelegate {
}

func panGestureDidEnd(translation: CGPoint, velocity: CGPoint) {
guard let initialFrame = initialFrame else { return }

let galleryHeight = initialFrame.height - translation.y

if galleryView.frame.height < GestureConstants.minimumHeight && velocity.y < 0 {
Expand Down