6

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

文档: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”的成员

额外的问题:为什么苹果没有location迅速曝光?

4

4 回答 4

4

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

import CoreLocation

LocationGetter.locationForAsset(ass)

编辑: Apple 已将其.location作为吸气剂包含在PHAsset. 获取位置现在就像asset.location.

于 2014-08-30T11:59:12.683 回答
1

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

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

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

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.
            }
        }
    }
}
于 2019-02-21T17:24:32.867 回答
0

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

var allAssets = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: nil)
    allAssets.enumerateObjectsUsingBlock({asset, index, stop in
        if let ass = asset as? PHAsset{
            println(ass.location)
        }
    }
于 2014-12-28T03:13:40.587 回答
0

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)
    }
}
于 2019-02-01T07:11:07.393 回答