如何将GIF图像添加到Assets文件夹并以编程方式将其加载到UIImageView中 [英] How to add GIF images to Assets folder and load them in UIImageView programmatically
问题描述
我正在尝试将@ 2x和@ 3x GIF图像放入Xcode中的Assets文件夹中.我尝试了以下链接,但对我而言不起作用. 链接1 和
I am trying to put my @2x and @3x GIF images into the Assets folder in Xcode. I have tried the following links but it didn't work for me. Link 1 and Link 2.
我当前正在通过将GIF文件添加到我的项目包并使用此文件访问文件来加载
I am currently loading the GIF files by adding them to my project bundle and accessing it using this
let bundleURL = NSBundle.mainBundle()
.URLForResource("phone_animation", withExtension: "gif")
但是我想从我的Assets文件夹中加载它们.有办法吗?将它们添加到素材资源后,如何将它们加载到我的imageview中?
But I want to load them from my Assets folder. Is there a way I can do it? And how do I load them into my imageview after adding it to Assets?
推荐答案
根据您的评论,这是您正在搜索的解决方案:
According to your comment, this is the solution that you are searching:
-
对使用SwiftGif的UIImage进行扩展:
make an extension of UIImage which uses SwiftGif:
extension UIImage {
public class func gif(asset: String) -> UIImage? {
if let asset = NSDataAsset(name: asset) {
return UIImage.gif(data: asset.data)
}
return nil
}
}
然后使用扩展名调整@Ganesh建议,您可能会执行以下操作:
Then tuning @Ganesh suggestion with the extension you might do something like:
imageView.image = UIImage.gif(asset: "YOUR_GIF_NAME_FROM_ASSET")
这篇关于如何将GIF图像添加到Assets文件夹并以编程方式将其加载到UIImageView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!