将带有地理位置数据的照片保存到照片库 Swift 3
Save photo with geolocation data to photo library Swift 3
如何使用地理位置元数据将照片保存到照片库?
我已请求(并允许)该应用访问用户位置:
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()})
}
}
在 iOS 8 Apple 推出 Photo Library.
以下是创建和更新 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
.
自行获取真实位置
嗯,我猜你需要先访问用户的位置,然后再调用 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()
这对我有用
如何使用地理位置元数据将照片保存到照片库?
我已请求(并允许)该应用访问用户位置:
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()})
}
}
在 iOS 8 Apple 推出 Photo Library.
以下是创建和更新 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
.
嗯,我猜你需要先访问用户的位置,然后再调用 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()
这对我有用