我注意到在iMessage中现在可以发送和显示动画GIF了。这是否意味着Apple现在支持在应用程序中显示动画GIF,还是最简单的方法仍然是将图像分成帧,然后按顺序显示它们?截至iOS 5.1,显示动画GIF的最简单方法是什么?
谢谢!
我注意到在iMessage中现在可以发送和显示动画GIF了。这是否意味着Apple现在支持在应用程序中显示动画GIF,还是最简单的方法仍然是将图像分成帧,然后按顺序显示它们?截至iOS 5.1,显示动画GIF的最简单方法是什么?
谢谢!
animatedImageNamed:duration:
。假设您要对旋转器进行动画处理,请将所有框架复制到项目中并按以下方式命名:spinner-1.png
- spinner-2.png
- spinner-3.png
- 以此类推[UIImage animatedImageNamed:@"spinner-" duration:1.0f];
此方法通过向提供的名称参数添加一系列数字来加载一系列文件。例如,如果名称参数包含“image”,则此方法将尝试从带有名称“image0”、“image1”等的文件中加载图像,一直到“image1024”。 动画图像中包含的所有图像应具有相同的大小和比例。
FLAnimatedImage是iOS的一款高效的开源动态GIF引擎:
这是一个经过充分测试的组件,我编写它是为了支持Flipboard中的所有GIF。
UIWebView
来显示动画GIF。如果该GIF将从服务器获取,则此操作会负责获取。它还适用于本地GIF。从 iOS 11 开始,照片框架允许添加动态GIF图像播放。
可以在此处下载示例应用程序here
有关动态GIF图像播放的更多信息(从13:35分钟开始):https://developer.apple.com/videos/play/wwdc2017/505/
#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
NSString *pathForGif = [[NSBundle mainBundle] pathForResource:@"ic_homepage_random0330" ofType: @"gif"];
NSData *gifData = [NSData dataWithContentsOfFile:pathForGif];
YYImage *img = [YYImage imageWithData:gifData];
YYAnimatedImageView *contentImageView = [[YYAnimatedImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
contentImageView.autoPlayAnimatedImage = YES;
contentImageView.image = img;
.png
格式。
2. 创建一个xcassets
档案,并为每个帧创建一个资源。确保差异只是后缀,应为gif的每个帧都应该有一个数字后缀(例如animated-image-1,animated-image-2等)。我使用单一比例使其更容易。
3. 将其加载到图像视图中:final class AnimatedImage: UIImageView {
init() {
super.init(frame: .zero)
let animatedImage = UIImage.animatedImageNamed("animated-image-", duration: 0.3)
translatesAutoresizingMaskIntoConstraints = false
image = animatedImage
NSLayoutConstraint.activate([
widthAnchor.constraint(equalToConstant: 150),
heightAnchor.constraint(equalTo: widthAnchor)
])
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}