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

前端之家收集整理的这篇文章主要介绍了动画 – 如何在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()
原文链接:https://www.f2er.com/swift/320012.html

猜你在找的Swift相关文章