在 iOS 中显示动画 GIF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9744682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display animated GIF in iOS
提问by swickblade
I noticed that with iMessage, animated gifs can now be sent and displayed. Does this mean that Apple is now supporting the display of animated GIFs in an application, or is the easiest method still to split the image in frames and then sequentially show them? What is the easiest way to display an animated GIF as of iOS 5.1?
我注意到使用 iMessage,现在可以发送和显示动画 gif。这是否意味着 Apple 现在支持在应用程序中显示动画 GIF,还是最简单的方法仍然是将图像分成帧然后依次显示?从 iOS 5.1 开始,显示动画 GIF 的最简单方法是什么?
Thanks!
谢谢!
回答by Joe Masilotti
If you are targeting iOS7 and already have the image split into frames you can use animatedImageNamed:duration:
.
如果您的目标是 iOS7 并且已经将图像拆分为多个帧,则可以使用animatedImageNamed:duration:
.
Let's say you are animating a spinner. Copy all of your frames into the project and name them as follows:
假设您正在为微调器设置动画。将所有框架复制到项目中,并按如下方式命名它们:
spinner-1.png
spinner-2.png
spinner-3.png
- etc.,
spinner-1.png
spinner-2.png
spinner-3.png
- 等,
Then create the image via:
然后通过以下方式创建图像:
[UIImage animatedImageNamed:@"spinner-" duration:1.0f];
从文档:
This method loads a series of files by appending a series of numbers to the base file name provided in the name parameter. For example, if the name parameter had ‘image' as its contents, this method would attempt to load images from files with the names ‘image0', ‘image1' and so on all the way up to ‘image1024'. All images included in the animated image should share the same size and scale.
此方法通过将一系列数字附加到 name 参数中提供的基本文件名来加载一系列文件。例如,如果 name 参数的内容为“image”,则此方法将尝试从名称为“image0”、“image1”等一直到“image1024”的文件中加载图像。动画图像中包含的所有图像应共享相同的大小和比例。
回答by Raphael Schaad
FLAnimatedImageis a performant open source animated GIF engine for iOS:
FLAnimatedImage是适用于 iOS 的高性能开源动画 GIF 引擎:
- Plays multiple GIFs simultaneously with a playback speed comparable to desktop browsers
- Honors variable frame delays
- Behaves gracefully under memory pressure
- Eliminates delays or blocking during the first playback loop
- Interprets the frame delays of fast GIFs the same way modern browsers do
- 以与桌面浏览器相当的播放速度同时播放多个 GIF
- 尊重可变帧延迟
- 在内存压力下表现得优雅
- 在第一个播放循环期间消除延迟或阻塞
- 以与现代浏览器相同的方式解释快速 GIF 的帧延迟
It's a well-tested component that I wrote to power all GIFs in Flipboard.
这是一个经过充分测试的组件,我编写它来支持 Flipboard 中的所有 GIF。
回答by Eric
I would recommend using the following code, it's much more lightweight, and compatible with ARC and non-ARC project, it adds a simple category on UIImageView:
我建议使用以下代码,它更轻量级,并且与 ARC 和非 ARC 项目兼容,它在 UIImageView 上添加了一个简单的类别:
回答by ThomasW
Another alternative is to use a UIWebView
to display the animated GIF. If the GIF is going to be fetched from a server, then this takes care of the fetching. It also works with local GIFs.
另一种选择是使用 aUIWebView
来显示动画 GIF。如果要从服务器获取 GIF,那么这将负责获取。它也适用于本地 GIF。
回答by sash
From iOS 11Photos framework allows to add animated Gifs playback.
从iOS 11照片框架允许添加动画 Gif 播放。
Sample app can be dowloaded here
示例应用程序可以在这里下载
More info about animated Gifs playback (starting from 13:35 min): https://developer.apple.com/videos/play/wwdc2017/505/
有关动画 Gif 播放的更多信息(从 13:35 开始):https: //developer.apple.com/videos/play/wwdc2017/505/
回答by Erik Holley
#import <QuickLook/QuickLook.h>
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
QLPreviewController *preview = [[QLPreviewController alloc] init];
preview.dataSource = self;
[self addChildViewController:preview];
[self.view addSubview:preview.view];
}
#pragma mark - QLPreviewControllerDataSource
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)previewController
{
return 1;
}
- (id)previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)idx
{
NSURL *fileURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"myanimated.gif" ofType:nil]];
return fileURL;
}
@end