动画 – 如何在Swift中将gif显示到UIImageView中?

我想在UIImageView中使用gif,但我不能.
我的代码是:
- (void)viewDidLoad {
    [super viewDidLoad];

    NSURL *url = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"gif"];
    UIImage *testimage = [UIImage animatedImageWithAnimatedGIFData:[NSData dataWithContentsOfURL:url]];
    self.dataimageView.animationImages = testimage.images;
    self.dataimageView.animationDuration = testimage.duration;
    self.dataimageView.animationRepeatCount = 1;
    self.dataimageView.image = testimage.images.lastObject;
    [self.dataimageView startAnimating];
}

迅速:

var url : NSURL = NSBundle.mainBundle().URLForResource("MAES",withExtension: "gif")

在目标C中我有animatedImageWithAnimatedGIFData,但是在swift中我不知道如何调用这个或者如果不存在..

谢谢!

我假设您正在使用 https://github.com/mayoff/uiimage-from-animated-gif for UIImage animatedImageWithAnimatedGIFData:并且源代码在Objective-C中.

您需要先将Objective-C标头导入Swift. Using Swift with Cocoa and Objective-C解释了如何执行此操作:

To import a set of Objective-C files in the same framework target as your Swift code,
you’ll need to import those files into the Objective-C umbrella header for the
framework.

To import Objective-C code into Swift from the same framework

  1. Under Build Settings,in Packaging,make sure the Defines Module setting for that framework target is set to Yes.
  2. In your umbrella header file,import every Objective-C header you want to expose to Swift. For example:

    #import "UIImage+animatedGIF.h"

然后,您可以按如下方式设置testimage

var testimage = UIImage.animatedImageWithAnimatedGIFData(
    NSData.dataWithContentsOfURL(url))
self.dataimageView.animationImages = testimage.images
self.dataimageView.animationDuration = testimage.duration
self.dataimageView.animationRepeatCount = 1
self.dataimageView.image = testimage.images.lastObject
self.dataimageView.startAnimating()

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...