如何使用地理位置元数据将照片保存到照片库?
我已请求(并允许)应用程序访问用户位置:
private func allowAccessToUserLocation() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
}
我是否需要请求相机应用程序的特定权限?
EDIT:
我使用 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()})
}
}
苹果在 iOS 8 中引入了照片库 https://developer.apple.com/reference/photos/phphotolibrary.
这是创建和更新的示例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
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)