diff --git a/Demo/ImagePickerDemo/Podfile.lock b/Demo/ImagePickerDemo/Podfile.lock index 7c584cda..af241783 100644 --- a/Demo/ImagePickerDemo/Podfile.lock +++ b/Demo/ImagePickerDemo/Podfile.lock @@ -11,4 +11,4 @@ EXTERNAL SOURCES: SPEC CHECKSUMS: ImagePicker: 32becfa25b8e9179e60c45411b577340d35e3e32 -COCOAPODS: 0.39.0.beta.4 +COCOAPODS: 0.38.2 diff --git a/Source/CameraView/CameraView.swift b/Source/CameraView/CameraView.swift index 93e247be..ccf50954 100644 --- a/Source/CameraView/CameraView.swift +++ b/Source/CameraView/CameraView.swift @@ -26,7 +26,7 @@ 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) @@ -34,6 +34,15 @@ class CameraView: UIViewController { 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 @@ -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) @@ -84,6 +93,8 @@ class CameraView: UIViewController { } } + captureDevice = capturedDevices?.firstObject as? AVCaptureDevice + if captureDevice != nil { beginSession() } @@ -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 }) }) @@ -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() { diff --git a/Source/Extensions/ConstraintsSetup.swift b/Source/Extensions/ConstraintsSetup.swift index 091e34f9..c0681b98 100644 --- a/Source/Extensions/ConstraintsSetup.swift +++ b/Source/Extensions/ConstraintsSetup.swift @@ -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)) } } diff --git a/Source/ImagePickerController.swift b/Source/ImagePickerController.swift index cbf53f16..7d3cece0 100644 --- a/Source/ImagePickerController.swift +++ b/Source/ImagePickerController.swift @@ -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 @@ -45,11 +43,6 @@ 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 @@ -57,12 +50,22 @@ public class ImagePickerController: UIViewController { 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 { @@ -82,6 +85,7 @@ public class ImagePickerController: UIViewController { view.backgroundColor = .whiteColor() view.backgroundColor = self.configuration.mainColor + cameraController.view.addGestureRecognizer(panGestureRecognizer) subscribe() setupConstraints() @@ -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 diff --git a/Source/TopView/TopView.swift b/Source/TopView/TopView.swift index 0140fc7b..2b38e9a1 100644 --- a/Source/TopView/TopView.swift +++ b/Source/TopView/TopView.swift @@ -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 } @@ -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 }()