【发布时间】:2017-06-29 12:14:24
【问题描述】:
我遇到了这个问题,我有一个相机和一个地图视图,如果使用应用程序拍摄照片,我想要做的就是获取位置,您可以在捕获图像的那一刻访问用户的位置并扩展 UIImage 以包含位置属性,并将此位置同时保存到地图视图或 tableView 上,当使用手机的相机拍摄照片时保存图像。感谢您的任何帮助。
这是我的代码。 (相机控制器)
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
}
}
地图视图控制器
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)
}
}
【问题讨论】:
-
那么你的问题是什么,什么不起作用?请解释您所面临的问题,而不仅仅是您想要实现的目标。
-
好吧,@DavidPasztor 问题是,我正在获取用户位置,但是当用户拍照时它并没有完全得到它。我要做的就是,当相机打开时,将用户位置同时获取到地图视图或tableView上,何时捕获
标签: ios swift mapkit core-location