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

Animation 如何在Swift中将gif显示到UIImageView中?,animation,swift,gif,ios8,Animation,Swift,Gif,Ios8,我想在UIImageView中使用gif,但我不能。 我的代码是: - (void)viewDidLoad { [super viewDidLoad]; NSURL *url = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"gif"]; UIImage *testImage = [UIImage animatedImageWithAnimatedGIFData:[NSData dataWit

我想在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")
在objective C中,我使用AnimateDimageWithAnimateDigifData创建了AnimateDimageWithAnimateDigifData,但在swift中,我不知道如何调用它,或者如果它不存在

谢谢

我假设您使用的是
+UIImage animatedImageWithAnimatedGIFData:
,源代码是Objective-C

您需要首先将Objective-C标题导入Swift说明如何执行此操作:

要在与Swift代码相同的框架目标中导入一组Objective-C文件, 您需要将这些文件导入到 框架

将Objective-C代码从同一框架导入Swift

  • 在“生成设置”下的“打包”中,确保该框架目标的“定义模块”设置设置设置为“是”
  • 在伞形头文件中,导入要向Swift公开的每个Objective-C头。例如:

    #导入“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()
    

    伟大的谢谢没关系。但我还有其他问题,有其他形式可以直接显示gif而不使用IImage+animatedGIF?UIImage不支持动画gif,但您可以像使用任何其他图像一样使用静止gif图像(
    UIImage(名为:“image.gif”)
    )。但是,如果可以将动画GIF拆分为单独的静止图像GIF,则可以将其加载到UIImage动画中,如下所示: