2017-06-29 3 views
1

私はカメラと地図ビューを持っていますが、画像をキャプチャした瞬間にユーザの場所にアクセスできるアプリを使って写真を撮ると、その場所を取得するだけです。 UIImageを拡張して場所のプロパティを追加し、同時にこの場所をマップビューまたはtableViewに保存して、電話機のカメラを使用して写真を撮影するときに画像を保存します。何か助けてくれてありがとう。Swift 3/4でカメラ内の位置を取得する方法は?

ここに私のコードです。 (カメラコントローラ)

import UIKit 

class ViewController: UIViewController, UINavigationControllerDelegate { 

    @IBOutlet weak var imageView: UIImageView! 
    @IBOutlet weak var resultLbl: UILabel! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    override func viewWillAppear(_ animated: Bool) { 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


    @IBAction func CamBtn(_ sender: Any) { 
     if !UIImagePickerController.isSourceTypeAvailable(.camera) { 
      return 
     } 

     let cameraPicker = UIImagePickerController() 
     cameraPicker.delegate = self 
     cameraPicker.sourceType = .camera 
     cameraPicker.allowsEditing = false 
     MapViewController.location.requestLocation() 
     guard let userLocation = MapViewController.location.location else {return} 
     present(cameraPicker, animated: true) 
    } 


    @IBAction func LibBtn(_ sender: Any) { 
     let picker = UIImagePickerController() 
     picker.allowsEditing = false 
     picker.delegate = self 
     picker.sourceType = .photoLibrary 
     present(picker, animated: true) 
    } 
} 
    extension ViewController: UIImagePickerControllerDelegate { 
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { 
     dismiss(animated: true, completion: nil) 
    } 

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
     picker.dismiss(animated: true) 
     resultLbl.text = "Analyzing Image..." 
     guard let image = info["UIImagePickerControllerOriginalImage"] as? UIImage else { 
      return 
     } 

} 

MapViewControllerあなたは写真の時点でユーザの位置を取りたい場合は、ユーザーは単に写真を撮影していない時に位置更新を要求する必要が

import Foundation 
import UIKit 
import MapKit 

class MapViewController: UIViewController, CLLocationManagerDelegate { 

    @IBOutlet weak var mapView: MKMapView! 
    @IBOutlet weak var tableView: UITableView! 

    let regionRadius: CLLocationDistance = 1000 
    var location: CLLocationManager! 
    let nearbyRequest = MKLocalSearchRequest() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     location = CLLocationManager() 
     location!.delegate = self 
     location.desiredAccuracy = kCLLocationAccuracyBest 
     location.requestAlwaysAuthorization() 
     location.startUpdatingLocation() 

     if CLLocationManager.authorizationStatus() == .authorizedWhenInUse { 
      location!.startUpdatingLocation() 
     } else { 
      location!.requestWhenInUseAuthorization() 
     } 
    } 

    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(true) 
     mapView.showsUserLocation = true 
    } 

    override func viewWillDisappear(_ animated: Bool) { 
     mapView.showsUserLocation = false 
    } 


    func location(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) { 
     switch status { 
     case .notDetermined: 
      print("Not Determined") 
     case .restricted: 
      print("Restricted") 
     case .denied: 
      print("Denied") 
     case .authorizedAlways: 
      print("AuthorizedAlways") 
     case .authorizedWhenInUse: 
      print("AuthorizedWhenInUse") 
     location!.startUpdatingLocation() 
     } 
    } 

    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) { 
     print("Failed to initialize Map: ", error.description) 
    } 

} 
+0

あなたの問題は何ですか、何が問題なのですか?あなたが達成しようとしているものだけでなく、何が問題であるかを説明してください。 –

+0

さて、@DavidPasztor問題は、私はユーザーの場所を取得していますが、それは非常にユーザーが写真を撮っているときにそれを取得していません。私がしたいのは、カメラが開いているときに、同時にマップビューまたはtableViewにユーザーの場所を取得することです。 –

答えて

0

他のViewControllerに位置情報の更新を開始してください。 CLLocationManager().requestLocation()を使用して1回限りの場所の更新をリクエストすることはできますが、これは非同期の呼び出しであることに注意してください。

この問題を軽減するために、あなたは非同期メソッドが終了したか、あなたがCLLocationManagerDelegatefunc locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])メソッドを使用して、すぐに位置更新が受信されると、画像/のMapViewを更新することができた場合にのみ、あなたの関数が返すために取得するPromiseKitを使用することができます。

変更var location: CLLocationManager!static var location = CLLocationManager()からMapViewControllerでは、locationは、typeプロパティではなく、暗黙的に開封されたインスタンスのプロパティであることで動作するようにそれに応じてMapViewControllerクラスを更新します。そのように続いてCameraControllerであなたの関数を変更します。

@IBAction func CamBtn(_ sender: Any) { 
     if !UIImagePickerController.isSourceTypeAvailable(.camera) { 
      return 
     } 

     let cameraPicker = UIImagePickerController() 
     cameraPicker.delegate = self 
     cameraPicker.sourceType = .camera 
     cameraPicker.allowsEditing = false 
     MapViewController.location.requestLocation() 
     guard let userLocation = MapViewController.location.location else {return} 
     present(cameraPicker, animated: true) 
    } 

これは完全な例ではありません、あなたはまだ、画像/のMapViewにuserLocationを追加する必要がありますが、自分でその部分を把握することができるはずです。また、この実装では、requestLocation()は、その値をチェックするまでに返されることは保証されていないので、PromiseKitCLLocationManager.promise()関数を使用したいと思います。

関連する問題