[英]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.