简体   繁体   English

如何在 iOS 应用程序中使用 GIF,而不使用 UIKit 在 Swift 5 中的任何第三方库?

[英]How to use GIFs inside an iOS App without any Third Party Library in Swift 5 using UIKit?

i am currently working on a project which depends heavily on GIFs.我目前正在从事一个严重依赖 GIF 的项目。 However, I never worked with GIFs in Swift for iOS before an am pretty lost;)但是,在迷失方向之前,我从未在 Swift 中为 iOS 使用过 GIF;)

The first thing I tried was to import some GIFs to my Assets.xcassets Folder .我尝试的第一件事是将一些 GIF 导入到我的Assets.xcassets Folder中。 I realised that using an ImageSet for GIFs does not work .我意识到对 GIF 使用 ImageSet 是行不通的

So my first Question: How do I import GIFs into my project.所以我的第一个问题:如何将 GIF 导入我的项目。 And should I use only one resolution or are three the better way like for images?我应该只使用一种分辨率还是三种更好的图像方式?

Then I already checked out some Questions about presenting a GIF inside an UIImageView , programatically and via Storyboard.然后我已经检查了一些关于在UIImageView中以编程方式和通过 Storyboard呈现 GIF 的问题。 As I figured out this is probably best possible by using an Extension for UIImage Class.正如我发现的那样,通过使用 UIImage Class 的扩展,这可能是最好的方法。 However, the Swift 4 Examples didn't work for me because of some reason;(但是,由于某种原因,Swift 4 示例对我不起作用;(

So my second Question: How do I use the imported GIFs inside my project and display them inside an UIImage View programatically?所以我的第二个问题:如何在我的项目中使用导入的 GIF,并以编程方式在 UIImage 视图中显示它们?

Thanks a lot for your help guys in advance!非常感谢您提前提供的帮助!

You have to manually decode GIF data, then you will be able to show it frame-by-frame.您必须手动解码 GIF 数据,然后才能逐帧显示。 You will not have any simple answers here, bcs that's definitely very hard idea, try copy decoding algorithm from Third-Party and player or just use it.在这里你不会有任何简单的答案,bcs 这绝对是一个非常难的主意,尝试从第三方和播放器复制解码算法,或者只是使用它。 Something like SDWebImage will be the best option for you.SDWebImage这样的东西将是您的最佳选择。 Check SDAnimatedImagePlayer, SDWebAnimatedImage classes.检查 SDAnimatedImagePlayer、SDWebAnimatedImage 类。

I also had this kind of problem to solve.我也有这样的问题要解决。 I found answers in this thread helpful.我发现此线程中的答案很有帮助。 Please check them out.请检查一下。 Add animated Gif image in Iphone UIImageView 在 Iphone UIImageView 中添加动画 Gif 图像

Also, I highly recommend to checkout some implementations inside FLAnimatedImage .另外,我强烈建议检查FLAnimatedImage中的一些实现。 It handles showing, playing and other functionalities you might need for GIFs in iOS.它处理 iOS 中的 GIF 可能需要的显示、播放和其他功能。

If your animated gif has a constant frame time for all frames then you can easily load it from your assets catalog without third party libraries with this:如果您的动画 gif 对所有帧都具有恒定的帧时间,那么您可以轻松地从资产目录中加载它,而无需使用第三方库:

extension UIImage {
    static func animatedGif(named: String, framesPerSecond: Double = 10) -> UIImage? {
        guard let asset = NSDataAsset(name: named) else { return nil }
        return animatedGif(from: asset.data, framesPerSecond: framesPerSecond)
    }

    static func animatedGif(from data: Data, framesPerSecond: Double = 10) -> UIImage? {
        guard let source = CGImageSourceCreateWithData(data as CFData, nil) else { return nil }
        let imageCount = CGImageSourceGetCount(source)
        var images: [UIImage] = []
        for i in 0 ..< imageCount {
            if let cgImage = CGImageSourceCreateImageAtIndex(source, i, nil) {
                images.append(UIImage(cgImage: cgImage))
            }
        }
        return UIImage.animatedImage(with: images, duration: Double(images.count) / framesPerSecond)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在没有任何第三方库的情况下在Swift 3.0中使用Alamofire解析JSON - How to parse JSON using Alamofire in Swift 3.0 without any third-party library 如何在不使用任何第三方库的情况下跟踪来自iOS中所有设备的生产应用程序错误/崩溃 - How to track production app bugs/crashes from all devices in iOS without using any third party libraries 如何在swift的iOS嵌入式动态框架中使用第三方库 - How to use third party lib in embedded dynamic framework for iOS with swift 如何在不使用第三方库的情况下在 swift2 中下载页面? - How to download a page in swift2 without using third party libraries? 如何在不使用第三方库的情况下在Facebook上共享链接? - how to share a link on facebook without using third party library? 没有第三方应用的iOS Universal深度链接 - iOS Universal deeplinking without third party app 如何在Swift 4中使用“ WWCalendarTimeSelector”第三方 - How to use “WWCalendarTimeSelector” third party in swift 4 如何测试使用UIKit的iOS库 - How to Test an iOS Library Which Is Using UIKit 在没有任何第三方的情况下,在 swift 中使用子类使 UITexftField 像 android 一样 - Make UITexftField like android using subclass in swift without any third party 如何在 iOS 上调试没有响应的第三方应用程序? - How to debug a third party app on iOS that is not responding?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM