You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PHPickerViewController中禁用照片位置信息?

如何在PHPickerViewController中排除照片的位置信息?

我正在使用PHPickerViewController实现用户向应用导入照片的功能,但它默认会显示包含照片位置信息的提示(见下方截图底部)。我完全不需要照片的位置信息,这会让用户误以为我的应用特意请求了位置权限,但实际上我并未这么做。我在其他应用中看不到该提示,不清楚如何实现这种行为。请问能否在使用PHPickerViewController时预先设置排除位置信息?

以下是我的配置代码:

// N.B. I also tried the zero-argument constructor PHPickerConfiguration()
var config = PHPickerConfiguration(photoLibrary: .shared())

config.filter = .images
config.selectionLimit = 0
config.selection = .ordered

let picker = PHPickerViewController(configuration: config)
picker.delegate = self

vc.present(picker, animated: true)

PHPickerViewController底部显示位置信息提示


你可以通过配置PHPickerConfiguration和资源请求选项来消除位置信息提示,同时确保不获取照片的位置元数据:

1. 修改PHPickerConfiguration,优先使用无元数据的表示

在你的配置代码中添加一行,指定优先使用当前照片的简化表示(不含额外元数据):

var config = PHPickerConfiguration(photoLibrary: .shared())
config.filter = .images
config.selectionLimit = 0
config.selection = .ordered
config.preferredAssetRepresentationMode = .current // 关键配置

let picker = PHPickerViewController(configuration: config)
picker.delegate = self

vc.present(picker, animated: true)

2. 在处理选中结果时,明确排除元数据

如果需要获取照片原始数据,在请求资源时设置shouldIncludeAssetMetadata为false,彻底屏蔽位置等元数据:

func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
    picker.dismiss(animated: true)
    
    for result in results {
        // 通过PHAsset获取原始数据的方式
        if let assetId = result.assetIdentifier,
           let asset = PHAsset.fetchAssets(withLocalIdentifiers: [assetId], options: nil).firstObject {
            guard let resource = PHAssetResource.assetResources(for: asset).first else { continue }
            
            let options = PHAssetResourceRequestOptions()
            options.shouldIncludeAssetMetadata = false // 排除所有元数据(含位置)
            
            PHAssetResourceManager.default().requestData(for: resource, options: options) { data, error in
                guard let data = data else { return }
                // 处理不含位置信息的照片数据
            }
        }
        
        // 仅获取UIImage的方式(默认不携带位置元数据)
        result.itemProvider.loadObject(ofClass: UIImage.self) { image, error in
            guard let image = image as? UIImage else { return }
            // 直接使用UIImage即可,无位置信息
        }
    }
}

关键说明

  • 设置preferredAssetRepresentationMode = .current会让系统优先提供不带额外元数据的照片版本,底部的位置提示会自动消失。
  • 通过PHAssetResourceRequestOptions关闭元数据,能从根源上避免获取到位置信息,同时也不会触发用户端的位置相关提示。

内容的提问来源于stack exchange,提问作者Bill

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 20:22:39