加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 05:45:05 所属栏目:百科 来源:网络整理
导读:我想在UIImageView中使用gif,但我不能. 我的代码是: - (void)viewDidLoad { [super viewDidLoad]; NSURL *url = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"gif"]; UIImage *testImage = [UIImage animatedImageWithAnimatedGIFData:[
我想在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()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读