私はカメラと地図ビューを持っていますが、画像をキャプチャした瞬間にユーザの場所にアクセスできるアプリを使って写真を撮ると、その場所を取得するだけです。 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)
}
}
あなたの問題は何ですか、何が問題なのですか?あなたが達成しようとしているものだけでなく、何が問題であるかを説明してください。 –
さて、@DavidPasztor問題は、私はユーザーの場所を取得していますが、それは非常にユーザーが写真を撮っているときにそれを取得していません。私がしたいのは、カメラが開いているときに、同時にマップビューまたはtableViewにユーザーの場所を取得することです。 –