尝试使用 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/