ios - 使用 SWIFT 以编程方式计算 assets.xcassets 路径

标签 ios objective-c swift path icloud

尝试使用 swift 代码计算图像的路径。我得到了我认为在 Objective-C 中有效的东西。

[UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.png", [[NSBundle mainBundle] resourcePath], imgName]];

为了获取我存储在 assets.xcassets 中的 .png 图像的路径。但我现在需要在 SWIFT 中这样做,而不是在 Objective-C 中。

我需要路径,因为我正在尝试将放在那里的图像上传到 iCloud 中的 CKAsset

最佳答案

如果您只需要文件的路径(而不是 UIImage 的实例),可以这样做:

swift 2:

if let resourcePath = NSBundle.mainBundle().resourcePath {
    let imgName = "dog.png"
    let path = resourcePath + "/" + imgName
}

swift 3/4:

if let resourcePath = Bundle.main.resourcePath {
    let imgName = "dog.png"
    let path = resourcePath + "/" + imgName
}

关于ios - 使用 SWIFT 以编程方式计算 assets.xcassets 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34444196/

相关文章:

ios - 当上一个 View 来自导航 Controller 时如何保留 TabBar 界面

javascript - InfoWindow 打开缓慢或根本不打开

objective-c - 以编程方式创建的 UIBarButtonItem 未启动选择器操作

ios - 如何在 Objective-C 中确定相同的 NSString?

objective-c - 在 iOS 5 上的 MPMusicPlayerController 上淡出播放音量

Swift字符串截取前一段字符

ios - UICollectionViewUpdateItem 操作 - 在 iOS6 上删除 Collection View 中的项目时无法识别的选择器

iOS 版 Safari 上的 JavaScript 类错误

ios - 在设备上测试时核心数据实体丢失

swift:添加具有相同高度的 UITextField 的 uistackView