如何显示GIF成的UIImageView斯威夫特? [英] How to show gif into UIImageView in Swift?

查看:274
本文介绍了如何显示GIF成的UIImageView斯威夫特?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用的UIImageView一个GIF,但我做不到。
我的code是:

   - (无效)viewDidLoad中{
[超级viewDidLoad中];NSURL * URL = [[一个NSBundle mainBundle] URLForResource:@测试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但在迅疾我不知道怎么称呼这个或如果不存在。

谢谢!


解决方案

我假设你正在使用的https://github.com/mayoff/uiimage-from-animated-gif +的UIImage animatedImageWithAnimatedGIFData:和源$ C ​​$ c是在Objective-C

您需要先导入您的Objective-C头文件到斯威夫特。 <一个href=\"https://developer.apple.com/library/$p$prelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html\">Using斯威夫特与可可和Objective-C 解释如何做到这一点:


  

要导入一组的Objective-C的文件放在同一框架的目标是你的雨燕code,
  你需要将这些文件导入到Objective-C的伞头的
  框架。


  
  

要导入的Objective-C code到雨燕来自同一框架


  
  

      
  1. 在生成设置,在包装,确保定义模块设置在该框架的目标设置为Yes。

  2.   
  3. 在你的伞头文件,导入要公开到每雨燕Objective-C的头。例如:


      
      

    #进口的UIImage + animatedGIF.h


  4.   

您可以再设置你的 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()

I want use a gif in UIImageView, but I can't. My code is:

 - (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")

In objective C I had animatedImageWithAnimatedGIFData but in swift I don't know how call this or if not exists..

Thanks!

解决方案

I assume you're using https://github.com/mayoff/uiimage-from-animated-gif for +UIImage animatedImageWithAnimatedGIFData: and the source code is in Objective-C.

You'll need to first import your Objective-C headers into Swift. Using Swift with Cocoa and Objective-C explains how to do this:

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"

You can then setup your testImage as follows:

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成的UIImageView斯威夫特?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆