【问题标题】:Save photo with geolocation data to photo library Swift 3将带有地理位置数据的照片保存到照片库 Swift 3
【发布时间】:2017-07-14 16:39:15
【问题描述】:

如何使用地理位置元数据将照片保存到照片库?

我已请求(并允许)该应用访问用户位置:

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()})
        }
    }

【问题讨论】:

  • 我不明白照片库中的位置和照片有什么关系?您能否修改您的问题并修复它以使其更清晰。
  • 能否请您在读取地理位置数据的位置添加代码?
  • 您在反转GeoCodeLocation 之前是否打印了位置?价值是多少?
  • 您是否使用 UIImagePickerControllerReferenceURL 键获取路径然后初始化 PHAsset?

标签: ios swift3 uiimagepickercontroller core-location


【解决方案1】:

在 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 自己获取真实位置。

【讨论】:

  • 这一行有错误...asset.location = location '(PHObjectPlaceholder类型的值没有成员位置)'
  • 抱歉,检查编辑,我通过删除专辑收藏简化了答案
【解决方案2】:

好吧,我想您需要先访问用户的位置,然后再致电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()

这对我有用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-29
    • 1970-01-01
    • 1970-01-01
    • 2017-10-11
    相关资源
    最近更新 更多