如何使用地理位置元数据将照片保存到照片库?
我已请求(并允许)该应用访问用户位置:
private func allowAccessToUserLocation() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
}
我是否需要询问相机应用程序的具体预设?
修改
我使用UIImagePickerController从应用程序中拍摄照片。 从应用程序中拍摄的所有照片都存储在照片库中,没有地理位置。
是否与将图像保存到照片库的方式有关? 我的问题在这里而不是核心位置权限吗?
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage{
UIImageWriteToSavedPhotosAlbum(pickedImage, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
dismiss(animated: true, completion: {
self.showPhoto()})
}
}
答案 0 :(得分:4)
在iOS 8中,Apple推出了Photo Library。
以下是使用location:
创建和更新PHAsset
的示例
func addAsset(image: UIImage, location: CLLocation? = nil) {
PHPhotoLibrary.shared().performChanges({
// Request creating an asset from the image.
let creationRequest = PHAssetChangeRequest.creationRequestForAsset(from: image)
// Set metadata location
if let location = location {
creationRequest.location = location
}
}, completionHandler: { success, error in
if !success { NSLog("error creating asset: \(error)") }
})
}
确保您在使用
保存之前有权访问照片PHPhotoLibrary.requestAuthorization(_:)
元数据可以通过以下方式阅读:
info[UIImagePickerControllerMediaMetadata]
我无法在我的测试中验证元数据存储位置,即使在允许位置服务的情况下也是如此。在这种情况下,使用CoreLocation
可以自己获得真实位置。
答案 1 :(得分:1)
我想你需要在致电func addAsset(image: UIImage, location: CLLocation? = nil) {...}
所以func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {....}
我在打电话:
let locationManager = CLLocationManager()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
else{
//Location service disabled"
}
//and then call....
addAsset(image: pickedImage, location: locationManager.location)
一旦我完成了:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {...}
我停止更新位置:
locationManager.stopUpdatingLocation()
这对我有用