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

use*_*232 13 path objective-c ios icloud swift

试图用快速代码计算图像的路径.我得到了这个,我认为在目标C中工作.

[UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.png", [[NSBundle mainBundle] resourcePath], imgName]];
Run Code Online (Sandbox Code Playgroud)

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

我需要路径,因为我试图上传我放在CKAssetiCloud中的图像.

Jam*_*ini 18

如果您只需要文件的路径(而不是实例UIImage),这将执行此操作:

斯威夫特2:

if let resourcePath = NSBundle.mainBundle().resourcePath {
    let imgName = "dog.png"
    let path = resourcePath + "/" + imgName
}
Run Code Online (Sandbox Code Playgroud)

迅捷3/4:

if let resourcePath = Bundle.main.resourcePath {
    let imgName = "dog.png"
    let path = resourcePath + "/" + imgName
}
Run Code Online (Sandbox Code Playgroud)

  • 我猜这次投票是因为它没有回答指定的问题.这不会提供对存储在xcassets中的图像文件的访问.这些图像被编译到名为Assets.car的文件中,因此无法通过路径名直接访问. (19认同)