使用 SWIFT 以编程方式计算 assets.xcassets 路径
Figuring assets.xcassets path programmatically using SWIFT
试图用 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
}
试图用 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
}