Skip to content

Commit

Permalink
Record location with CLLocationManager if permission given by user
Browse files Browse the repository at this point in the history
  • Loading branch information
fnakstad committed Mar 15, 2016
1 parent 14e2857 commit 162bfe8
Showing 1 changed file with 45 additions and 7 deletions.
52 changes: 45 additions & 7 deletions Source/CameraView/CameraView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ protocol CameraViewDelegate: class {
func imageToLibrary()
}

class CameraView: UIViewController {
class CameraView: UIViewController, CLLocationManagerDelegate {

lazy var blurView: UIVisualEffectView = { [unowned self] in
let effect = UIBlurEffect(style: .Dark)
Expand Down Expand Up @@ -83,10 +83,14 @@ class CameraView: UIViewController {
var stillImageOutput: AVCaptureStillImageOutput?
var animationTimer: NSTimer?

var locationManager = CLLocationManager()
var latestLocation: CLLocation?

override func viewDidLoad() {
super.viewDidLoad()

initializeCamera()
initializeLocationManager()

view.backgroundColor = Configuration.mainColor
previewLayer?.backgroundColor = Configuration.mainColor.CGColor
Expand All @@ -100,6 +104,11 @@ class CameraView: UIViewController {
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
setCorrectOrientationToPreviewLayer()
locationManager.startUpdatingLocation()
}

override func viewDidDisappear(animated: Bool) {
locationManager.stopUpdatingLocation()
}

// MARK: - Layout
Expand Down Expand Up @@ -153,6 +162,14 @@ class CameraView: UIViewController {
if captureDevice != nil { beginSession() }
}

// MARK: - Initialize location manager

func initializeLocationManager() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
}

// MARK: - Actions

func settingsButtonDidTap() {
Expand Down Expand Up @@ -248,14 +265,17 @@ class CameraView: UIViewController {
stillImageOutput.captureStillImageAsynchronouslyFromConnection(stillImageOutput.connectionWithMediaType(AVMediaTypeVideo),
completionHandler: { (buffer, error) -> Void in
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer)

guard let imageFromData = UIImage(data: imageData) else { return }
UIImageWriteToSavedPhotosAlbum(imageFromData, self, "saveImage:error:context:", nil)
})
})
}

func saveImage(image: UIImage, error: NSErrorPointer, context:UnsafePointer<Void>) {
delegate?.imageToLibrary()
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
let request = PHAssetChangeRequest.creationRequestForAssetFromImage(imageFromData)
request.location = self.latestLocation
}, completionHandler: { success, error in
self.delegate?.imageToLibrary()
})
})
})
}

// MARK: - Timer methods
Expand Down Expand Up @@ -336,6 +356,24 @@ class CameraView: UIViewController {
captureSession.addOutput(stillImageOutput)
}

// MARK: - CLLocationManagerDelegate

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// Pick the location with best (= smallest value) horizontal accuracy
latestLocation = locations.sort{ $0.horizontalAccuracy < $1.horizontalAccuracy }.first
}

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedAlways || status == .AuthorizedWhenInUse {
locationManager.startUpdatingLocation()
} else {
locationManager.stopUpdatingLocation()
}
}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
}

// MARK: - Private helpers

func showNoCamera(show: Bool) {
Expand Down

0 comments on commit 162bfe8

Please sign in to comment.