gpt4 book ai didi

animation - 如何在 Swift 中将 gif 显示到 UIImageView 中?

转载 作者:搜寻专家 更新时间:2023-10-30 22:26:38 25 4
gpt4 key购买 nike

我想在 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];
}

swift :

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

在 objective-c 中我有 animatedImageWithAnimatedGIFData 但很快我不知道如何调用它或者如果不存在..

谢谢!

最佳答案

我假设您使用的是 https://github.com/mayoff/uiimage-from-animated-gif +UIImage animatedImageWithAnimatedGIFData: 源代码在 Objective-C 中。

您需要先将 Objective-C header 导入 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()

关于animation - 如何在 Swift 中将 gif 显示到 UIImageView 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24485308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com