如何将 GIF 图像添加到 Assets 文件夹并以编程方式将它们加载到 UIImageView [英] How to add GIF images to Assets folder and load them in UIImageView programmatically

查看:56
本文介绍了如何将 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:

  1. 使用 SwiftGif 对 UIImage 进行扩展:

  1. 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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆