简体   繁体   English

在Swift中获取PHAsset的位置

[英]Getting the location of a PHAsset in Swift

For some reason the location property on a PHAsset is only exposed in Objective-c and not in Swift. 由于某种原因, PHAsset上的location属性仅在Objective-c中公开,而不在Swift中公开。

Documentation: PHAsset.location 文档:PHAsset.location

To work around it, I figured I could create a Objective-C class whose sole purpose is to extract the location and import it into Swift. 为了解决这个问题,我想我可以创建一个Objective-C类,其唯一目的是提取位置并将其导入Swift。

LocationGetter.h LocationGetter.h

@interface LocationGetter : NSObject
+ (CLLocation *)locationForAsset:(PHAsset *) asset;
@end

LocationGetter.m LocationGetter.m

@implementation LocationGetter
+ (CLLocation *)locationForAsset:(PHAsset *) asset {
    return [asset location];
}
@end

So far so good, but when I try to use it in Swift: 到目前为止一切都那么好,但是当我尝试在Swift中使用它时:

LocationGetter.locationForAsset(ass)

'LocationGetter.Type' does not have a member named 'locationForAsset' 'LocationGetter.Type'没有名为'locationForAsset'的成员

Bonus question: Why on earth didn't Apple expose location in swift? 奖金问题:为什么苹果公司没有在快速公开location

It turns out that the answer is really simple. 事实证明,答案非常简单。 The problem is that the Swift file don't know what a CLLocation is, and thus refuses to import that function. 问题是Swift文件不知道CLLocation是什么,因此拒绝导入该函数。 Importing CoreLocation solves the issue. 导入CoreLocation可以解决此问题。

import CoreLocation

LocationGetter.locationForAsset(ass)

EDIT: Apple has since included .location as a getter on the PHAsset . 编辑:苹果公司,此后他先后.location作为对一个getter PHAsset Getting the location is now as easy as asset.location . 获取位置现在就像asset.location一样简单。

For those who looking to print each photo location, here it is: 对于那些希望打印每个照片位置的人来说,这里是:

var allAssets = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: nil)
    allAssets.enumerateObjectsUsingBlock({asset, index, stop in
        if let ass = asset as? PHAsset{
            println(ass.location)
        }
    }

You can retrieve the location of every PHAsset as easy as these lines of code: 您可以像这些代码行一样轻松检索每个PHAsset的位置:

let phFetchRes = PHAsset.fetchAssets(with: PHAssetMediaType.image , options: nil) // Fetch all PHAssets of images from Camera roll
let asset = phFetchRes.object(at: 0) // retrieve cell 0 as a asset 
let location = asset.location // retrieve the location
print(location) // Print result

Or if you want to retrieve all of locations from PHAsset you can use above codes like this: 或者,如果要从PHAsset中检索所有位置,可以使用以下代码:

let phFetchRes = PHAsset.fetchAssets(with: PHAssetMediaType.image , options: nil) // Fetch all PHAssets of images from Camera roll


phFetchRes.enumerateObjectsUsingBlock({asset, index, stop in
    if let ass = asset as? PHAsset{
        println(ass.location)
    }
}

iOS12, Swift 4 - Get location from Photo Library Moment, if asset itself does not have location. iOS12,Swift 4 - 如果资产本身没有位置,从Photo Library Moment获取位置。

I noticed that sometimes, the location of the asset itself was nil, while, in the Photo's app, the asset was grouped into a moment that had a location. 我注意到有时候,资产本身的位置是零,而在Photo的应用程序中,资产被分组到一个有位置的时刻。 If I had to guess, I'd say the photo app groups photos by date into a moment and then if at least one of those photos has a location, the moment is given a location. 如果我不得不猜测,我会说照片应用程序按照日期将照片分组到片刻,然后如果这些照片中至少有一张有位置,则会给出一个位置。

Now, how to get the location of that moment, if the location of the asset itself is nil? 现在,如果资产本身的位置为零,如何获得该时刻的位置? Like this: 像这样:

if let asset = info[UIImagePickerController.InfoKey.phAsset] as? PHAsset {
    if let photoCoordinate = asset.location?.coordinate {
        // The asset itself has a location. Do something with it.
    }
    else {
        // The asset itself does not have a location
        // find the moments containing the asset
        let momentsContainingAsset = PHAssetCollection.fetchAssetCollectionsContaining(asset, with: .moment, options: nil)
        for i in 0..<momentsContainingAsset.count {
            let moment = momentsContainingAsset.object(at: i)
            if let momentCoordinate = moment.approximateLocation?.coordinate {
                // this moment has a location. Use it as you wish.
            }
        }
    }
}

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

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