热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在Swift中获取PHAsset的位置-GettingthelocationofaPHAssetinSwift

ForsomereasonthelocationpropertyonaPHAssetisonlyexposedinObjective-candnotinSwift.

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

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

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

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?

奖金问题:为什么Apple没有公开位置?

2 个解决方案

#1


4  

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. Importing CoreLocation solves the issue.

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

import CoreLocation

LocationGetter.locationForAsset(ass)

EDIT: Apple has since included .location as a getter on the PHAsset. Getting the location is now as easy as asset.location.

编辑:Apple已将.location列为PHAsset的吸气剂。获取位置现在就像asset.location一样简单。

#2


0  

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

推荐阅读
author-avatar
wszr12345597
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有