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

Improve/usability #23

Merged
merged 10 commits into from
Sep 8, 2015
2 changes: 1 addition & 1 deletion Demo/ImagePickerDemo/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
ImagePicker: 32becfa25b8e9179e60c45411b577340d35e3e32

COCOAPODS: 0.39.0.beta.4
COCOAPODS: 0.38.2
43 changes: 36 additions & 7 deletions Source/CameraView/CameraView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,23 @@ class CameraView: UIViewController {
lazy var focusImageView: UIImageView = { [unowned self] in
let imageView = UIImageView()
imageView.image = self.getImage("focusIcon")
imageView.backgroundColor = UIColor.clearColor()
imageView.backgroundColor = .clearColor()
imageView.frame = CGRectMake(0, 0, 110, 110)
imageView.alpha = 0
self.view.addSubview(imageView)

return imageView
}()

lazy var capturedImageView: UIView = { [unowned self] in
let view = UIView()
view.backgroundColor = .blackColor()
view.alpha = 0
self.view.addSubview(view)

return view
}()

lazy var containerView: UIView = {
let view = UIView()
view.alpha = 0
Expand Down Expand Up @@ -62,7 +71,7 @@ class CameraView: UIViewController {
// MARK: - Initialize camera

func initializeCamera() {
captureSession.sessionPreset = AVCaptureSessionPreset640x480
captureSession.sessionPreset = AVCaptureSessionPreset1280x720
capturedDevices = NSMutableArray()

let authorizationStatus = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo)
Expand All @@ -84,6 +93,8 @@ class CameraView: UIViewController {
}
}

captureDevice = capturedDevices?.firstObject as? AVCaptureDevice

if captureDevice != nil {
beginSession()
}
Expand Down Expand Up @@ -118,7 +129,7 @@ class CameraView: UIViewController {
self.captureSession.removeInput(currentDeviceInput)
do { try self.captureSession.addInput(AVCaptureDeviceInput(device: self.captureDevice)) } catch {}
self.captureSession.commitConfiguration()
UIView.animateWithDuration(1.3, animations: { [unowned self] in
UIView.animateWithDuration(0.7, animations: { [unowned self] in
self.containerView.alpha = 0
})
})
Expand All @@ -144,22 +155,40 @@ class CameraView: UIViewController {
}

func takePicture() {
capturedImageView.frame = view.bounds

UIView.animateWithDuration(0.1, animations: {
self.capturedImageView.alpha = 1
}, completion: { _ in
UIView.animateWithDuration(0.1, animations: {
self.capturedImageView.alpha = 0
})
})

let queue = dispatch_queue_create("session queue", DISPATCH_QUEUE_SERIAL)
let videoOrientation = previewLayer?.connection.videoOrientation

stillImageOutput?.connectionWithMediaType(AVMediaTypeVideo).videoOrientation = videoOrientation!

dispatch_async(queue, { [unowned self] in
self.stillImageOutput!.captureStillImageAsynchronouslyFromConnection(self.stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo), completionHandler: { (buffer, error) -> Void in
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer)
let image = UIImage(data: imageData)
self.delegate?.imageToLibrary(image!)
let orientation = ALAssetOrientation(rawValue: image!.imageOrientation.rawValue)
let image = self.cropImage(UIImage(data: imageData)!)
let orientation = ALAssetOrientation(rawValue: 3)
let assetsLibrary = ALAssetsLibrary()
assetsLibrary.writeImageToSavedPhotosAlbum(image!.CGImage, orientation: orientation!, completionBlock: nil)
self.delegate?.imageToLibrary(image)
assetsLibrary.writeImageToSavedPhotosAlbum(image.CGImage, orientation: orientation!, completionBlock: nil)
})
})
}

func cropImage(image: UIImage) -> UIImage {
let imageReference = CGImageCreateWithImageInRect(image.CGImage, CGRect(x: 0, y: 0, width: image.size.height - 200, height: image.size.width))
let normalizedImage = UIImage(CGImage: imageReference!, scale: 1, orientation: .Right)

return normalizedImage
}

// MARK: - Timer methods

func timerDidFire() {
Expand Down
10 changes: 9 additions & 1 deletion Source/Extensions/ConstraintsSetup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,19 @@ extension TopView {

addConstraint(NSLayoutConstraint(item: rotateCamera, attribute: .Right,
relatedBy: .Equal, toItem: self, attribute: .Right,
multiplier: 1, constant: -Dimensions.rightOffset))
multiplier: 1, constant: Dimensions.rightOffset))

addConstraint(NSLayoutConstraint(item: rotateCamera, attribute: .CenterY,
relatedBy: .Equal, toItem: self, attribute: .CenterY,
multiplier: 1, constant: 0))

addConstraint(NSLayoutConstraint(item: rotateCamera, attribute: .Width,
relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute,
multiplier: 1, constant: 55))

addConstraint(NSLayoutConstraint(item: rotateCamera, attribute: .Height,
relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute,
multiplier: 1, constant: 55))
}
}

Expand Down
33 changes: 25 additions & 8 deletions Source/ImagePickerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ public class ImagePickerController: UIViewController {
static let velocity: CGFloat = 100
}

public var stack = ImageStack()

lazy public var galleryView: ImageGalleryView = { [unowned self] in
let galleryView = ImageGalleryView()
galleryView.delegate = self
Expand All @@ -45,24 +43,29 @@ public class ImagePickerController: UIViewController {
return view
}()

lazy var configuration: PickerConfiguration = {
let configuration = PickerConfiguration()
return configuration
}()

lazy var cameraController: CameraView = { [unowned self] in
let controller = CameraView()
controller.delegate = self

return controller
}()

lazy var panGestureRecognizer: UIPanGestureRecognizer = { [unowned self] in
let gesture = UIPanGestureRecognizer()
gesture.addTarget(self, action: "panGestureRecognizerHandler:")

return gesture
}()

lazy var configuration: PickerConfiguration = PickerConfiguration()

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!
public weak var delegate: ImagePickerDelegate?

public var doneButtonTitle: String? {
didSet {
Expand All @@ -82,6 +85,7 @@ public class ImagePickerController: UIViewController {

view.backgroundColor = .whiteColor()
view.backgroundColor = self.configuration.mainColor
cameraController.view.addGestureRecognizer(panGestureRecognizer)

subscribe()
setupConstraints()
Expand Down Expand Up @@ -275,6 +279,19 @@ extension ImagePickerController: ImageGalleryPanGestureDelegate {
numberOfCells = Int(initialContentOffset.x / galleryView.collectionSize.width)
}

func panGestureRecognizerHandler(gesture: UIPanGestureRecognizer) {
let translation = gesture.translationInView(view)
let velocity = gesture.velocityInView(view)

if gesture.locationInView(view).y > galleryView.frame.origin.y - 25 {
gesture.state == .Began ? panGestureDidStart() : panGestureDidChange(translation)
}

if gesture.state == .Ended {
panGestureDidEnd(translation, velocity: velocity)
}
}

func panGestureDidChange(translation: CGPoint) {
let galleryHeight = initialFrame.height - translation.y

Expand Down
3 changes: 2 additions & 1 deletion Source/TopView/TopView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TopView: UIView {

struct Dimensions {
static let leftOffset: CGFloat = 11
static let rightOffset: CGFloat = 11
static let rightOffset: CGFloat = 7
static let height: CGFloat = 34
}

Expand All @@ -32,6 +32,7 @@ class TopView: UIView {
let button = UIButton()
button.setImage(self.getImage("cameraIcon"), forState: .Normal)
button.addTarget(self, action: "rotateCameraButtonDidPress:", forControlEvents: .TouchUpInside)
button.imageView?.contentMode = .Center

return button
}()
Expand Down