简体   繁体   English

在 UIImageView IOS 中加载动画 gif

[英]Load Animated gif in UIImageView IOS

I have this gif into my assets file我有这个 gif 到我的资产文件中动图

The name of the assets is loading_apple but when I add the following code I get a nullpointer exception error:资产的名称是 loading_apple 但当我添加以下代码时,我收到空指针异常错误:

let img = UIImage (named: "loading_apple")

So how can I show this gif ?那么我怎样才能显示这个 gif 呢? :( :(

Hope someone can help me.希望可以有人帮帮我。

我建议使用 FLAnimatedImage https://github.com/Flipboard/FLAnimatedImage

I would recommend breaking out the frames of that gif and use animatedImageNamed:duration: - you can name them all the similar name with a number change at the end.我建议打破该 gif 的框架并使用animatedImageNamed:duration: - 您可以将它们命名为所有相似的名称,并在末尾更改数字。 For instance:例如:

loading-1.png loading-2.png loading-3.png etc. loading-1.png loading-2.png loading-3.png

Xcode will recognize you want multiple images and will play those through in order. Xcode 会识别出您需要多张图片,并按顺序播放这些图片。

Look at THIS看看这个

instead of storing the gif file, why don't u make use of CAReplicatorLayer refer this link and this link in objective c , i took same code in second like and with some modification,而不是存储 gif 文件,你为什么不使用CAReplicatorLayer参考这个链接目标 c 中的这个链接,我在第二个中使用了相同的代码并进行了一些修改,

func spinTheCustomSpinner() -> Void {
    let aBar:CALayer = CALayer.init()
    aBar.bounds = CGRectMake(0, 0, 8, 25);
    aBar.cornerRadius = 4; //(8/2)
    aBar.backgroundColor = UIColor.blackColor().CGColor
    aBar.position = CGPointMake(150.0, 150.0 + 35)

    let replicatorLayer:CAReplicatorLayer = CAReplicatorLayer.init()
    replicatorLayer.bounds = CGRectMake(0, 0,300,300)
    replicatorLayer.cornerRadius = 10.0
    replicatorLayer.backgroundColor = UIColor.whiteColor().CGColor
    replicatorLayer.position = CGPointMake(CGRectGetMidX(self.view!.bounds), CGRectGetMidY(self.view!.bounds))
    let angle:CGFloat = CGFloat (2.0 * M_PI) / 12.0
    let transform:CATransform3D = CATransform3DMakeRotation(angle, 0, 0, 1.0)
    replicatorLayer.instanceCount = 12
    replicatorLayer.instanceTransform = transform
    replicatorLayer .addSublayer(aBar)
    self.view!.layer .addSublayer(replicatorLayer)

    aBar.opacity = 0.0
    let animationFade:CABasicAnimation = CABasicAnimation(keyPath: "opacity")
    animationFade.fromValue = NSNumber(float: 1.0)
    animationFade.toValue = NSNumber(float: 0.0)
    animationFade.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
    animationFade.repeatCount = HUGE
    animationFade.duration = 1.0

    let aBarAnimationDuration:Double = 1.0/12.0
    replicatorLayer.instanceDelay = aBarAnimationDuration
    aBar .addAnimation(animationFade, forKey: "fadeAnimation")

}

if u use above with a view, show this view while loading and hide after loading, this is really cool and handy and there is no headache of storing the gif file and using a image view to load.如果你在上面使用一个视图,在加载时显示这个视图并在加载后隐藏,这真的很酷很方便,而且没有存储 gif 文件和使用图像视图加载的麻烦。

and by changing the properties of aBar layer u get different effects.并通过更改aBar图层的属性, aBar可以获得不同的效果。

对于这个特定的 gif,您可能想要使用UIActivityIndicatorView并使用startAnimating()使其动画化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM