繁体   English   中英

如何在iOS上从照相馆拍摄图像的地方(不是GPS坐标)?

[英]How to get place where image from photo gallery was taken (not GPS coordinates) on iOS?

iOS上“照片”应用程序中的图像有时与拍摄照片的位置有关。 例如,城市名称,甚至是城市中的特定地点,例如西雅图的派克市场。 此地点在iOS相册应用的导航栏中显示为图像标题。

如何获得该元数据?

我正在使用PHAssetCollection.FetchAssetCollections方法访问照片。

您可以直接从PHAsset对象获取位置。 获取地点信息。 即地址使用CoreLocation框架

func reverseGeoCoding(asset: PHAsset) {
    let location = asset.location

    let geocoder = CLGeocoder()
    geocoder.reverseGeocodeLocation(location) { (placemarks, error) in

        if let error = error {
            print("Geo Coding Error: \(error.localizedDescription)")
            return
        }

        guard let placemarks = placemarks,
            let firstPlacemark = placemarks.first,
            let addressDictionary = firstPlacemark.addressDictionary else { return }
        //            let street = addressDictionary["Street"]
        //            let city = addressDictionary["City"]
        //            let state = addressDictionary["State"]
        //            let zip = addressDictionary["ZIP"]
        print(addressDictionary.description)
        if let array = addressDictionary["FormattedAddressLines"] as? [Any] {
            let address = array.map { "\($0)" }.joined(separator: ",\n")
            print("Address : \(address)")
        }   
    }
}

更新:

对于地址,您可以直接从CLPlacemark对象获取地址

请参阅参考资料: https : //developer.apple.com/documentation/corelocation/clplacemark

从之前的代码中, let firstPlacemark = placemarks.first

firstPlacemark.propertyfirstPlacemark.property属性)

// CLPlacemark properties

open var name: String? { get } // eg. Apple Inc.

open var thoroughfare: String? { get } // street name, eg. Infinite Loop

open var subThoroughfare: String? { get } // eg. 1

open var locality: String? { get } // city, eg. Cupertino

open var subLocality: String? { get } // neighborhood, common name, eg. Mission District

open var administrativeArea: String? { get } // state, eg. CA

open var subAdministrativeArea: String? { get } // county, eg. Santa Clara

open var postalCode: String? { get } // zip code, eg. 95014

open var isoCountryCode: String? { get } // eg. US

open var country: String? { get } // eg. United States

open var inlandWater: String? { get } // eg. Lake Tahoe

open var ocean: String? { get } // eg. Pacific Ocean

open var areasOfInterest: [String]? { get } // eg. Golden Gate Park

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM