【问题标题】:Getting the location of a PHAsset in Swift在 Swift 中获取 PHAsset 的位置
【发布时间】:2014-10-24 06:25:50
【问题描述】:

由于某种原因,PHAsset 上的 location 属性仅在 Objective-c 中公开,而不在 Swift 中公开。

Documentation: PHAsset.location

为了解决这个问题,我想我可以创建一个 Objective-C 类,其唯一目的是提取位置并将其导入 Swift。

LocationGetter.h

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

LocationGetter.m

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

到目前为止一切都很好,但是当我尝试在 Swift 中使用它时:

LocationGetter.locationForAsset(ass)

“LocationGetter.Type”没有名为“locationForAsset”的成员

额外问题:为什么 Apple 没有迅速公开 location

【问题讨论】:

  • 你能分享更多代码吗?出于某种原因,这对我来说崩溃了。并非我所有的资产都有一个位置,也许这​​就是原因,但我不知道如何在你提供的目标 c 函数中处理这个问题,非常感谢帮助。
  • @alexsalo Apple 似乎已经修复了 Swift 中的 location 访问器。您应该可以通过 ass.location 获取位置。 ass 是您的 PHAsset。 Documentation: PHAsset.location
  • 是的,你说的很对,当时我在展开时遇到了问题。再次感谢!

标签: ios objective-c swift ios8 phasset


【解决方案1】:

事实证明,答案真的很简单。问题是 Swift 文件不知道 CLLocation 是什么,因此拒绝导入该函数。导入CoreLocation 解决了这个问题。

import CoreLocation

LocationGetter.locationForAsset(ass)

编辑: Apple 已将 .location 作为吸气剂添加到 PHAsset 中。获取位置现在就像asset.location 一样简单。

【讨论】:

  • 如果真的解决了问题,别忘了将自己的答案标记为解决方案
  • 真的吗?我不知道...对不起
【解决方案2】:

iOS12、Swift 4 - 如果资产本身没有位置,则从照片库时刻获取位置。

我注意到有时,资产本身的位置为零,而在照片的应用程序中,资产被分组到具有位置的时刻。如果我不得不猜测,我会说照片应用程序按日期将照片分组到一个时刻,然后如果其中至少一张照片有一个位置,那么这个时刻就会被赋予一个位置。

现在,如果资产本身的位置为零,如何获得那个时刻的位置?像这样:

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.
            }
        }
    }
}

【讨论】:

    【解决方案3】:

    对于那些希望打印每个照片位置的人,这里是:

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

    【讨论】:

      【解决方案4】:

      您可以像以下代码行一样简单地检索每个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
      

      或者,如果您想从 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)
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-02-24
        • 1970-01-01
        • 1970-01-01
        • 2019-02-13
        • 1970-01-01
        • 2017-07-03
        • 2017-06-29
        相关资源
        最近更新 更多