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

动画 – 如何在Swift中将gif显示到UIImageView中?,第1张

概述我想在UIImageView中使用gif,但我不能. 我的代码是: - (void)viewDidLoad { [super viewDidLoad]; NSURL *url = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"gif"]; UIImage *testImage = [UIImag 我想在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

Under Build Settings,in Packaging,make sure the defines Module setting for that framework target is set to Yes.

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.imagesself.dataimageVIEw.animationDuration = testimage.durationself.dataimageVIEw.animationRepeatCount = 1self.dataimageVIEw.image = testimage.images.lastObjectself.dataimageVIEw.startAnimating()
总结

以上是内存溢出为你收集整理的动画 – 如何在Swift中将gif显示到UIImageView中?全部内容,希望文章能够帮你解决动画 – 如何在Swift中将gif显示到UIImageView中?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1048697.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存