How to add GIF images to Assets folder and load them in UIImageView programmatically(如何将 GIF 图像添加到 Assets 文件夹并以编程方式将它们加载到 UIImageView)
问题描述
我正在尝试将我的 @2x 和 @3x GIF 图像放入 Xcode 的 Assets 文件夹中.我已经尝试了以下链接,但它对我不起作用.链接 1 和 链接2.
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 文件夹中加载它们.有什么办法可以做到吗?以及如何在将它们添加到 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将 GIF 图像添加到 Assets 文件夹并以编程方式将它们加载到 UIImageView
基础教程推荐
- iOS4 创建后台定时器 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
