Ios 将带有地理位置数据的照片保存到照片库Swift 3
如何使用地理位置元数据将照片保存到照片库 我已请求(并允许)应用访问用户位置:Ios 将带有地理位置数据的照片保存到照片库Swift 3,ios,swift3,uiimagepickercontroller,core-location,Ios,Swift3,Uiimagepickercontroller,Core Location,如何使用地理位置元数据将照片保存到照片库 我已请求(并允许)应用访问用户位置: private func allowAccessToUserLocation() { locationManager = CLLocationManager() locationManager.delegate = self locationManager.requestWhenInUseAuthorization() } func addAsset(ima
private func allowAccessToUserLocation() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
}
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)") }
})
}
locationManager.stopUpdatingLocation()
我需要为相机应用程序询问具体的任务吗
编辑:
我使用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中,苹果推出了
以下是使用位置创建和更新PHAsset
的示例:
private func allowAccessToUserLocation() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
}
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)") }
})
}
locationManager.stopUpdatingLocation()
使用保存前,请确保您有权访问照片
PHPhotoLibrary.requestAuthorization(_:)
元数据可以通过以下方式读取:
info[UIImagePickerControllerMediaMetadata]
我无法验证元数据是否存储位置,在我的测试中,甚至不允许使用位置服务。在这种情况下,use可以通过使用
CoreLocation
自己获得实际位置。我想在调用func addAsset(image:UIImage,location:CLLocation?=nil){…}
因此,在func imagePickerController(picker:UIImagePickerController,didFinishPickingMediaWithInfo:[字符串:任意])中的某个地方,{..}
我打电话:
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]) {...}
我停止更新位置:
private func allowAccessToUserLocation() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
}
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)") }
})
}
locationManager.stopUpdatingLocation()
这对我很有用我不明白照片库中的位置和照片之间的关系是什么?你能修改一下你的问题并把它修改得更清楚吗?你能在你读取地理位置数据的地方添加代码吗?你在反向地理位置之前打印了位置吗?值是多少?是否使用UIImagePickerControllerReferenceURL键获取路径,然后初始化相位集?此行有一个错误。。。asset.location=location'(PHObjectPlaceholder类型的值没有成员位置)对不起,请选中编辑,我已通过删除相册集合简化了答案