4

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:

let img = UIImage (named: "loading_apple")

So how can I show this gif ? :(

Hope someone can help me.

Willy Cornejo
  • 105
  • 1
  • 3
  • 10

4 Answers4

2

I would recommend using FLAnimatedImage https://github.com/Flipboard/FLAnimatedImage

Vasanth
  • 420
  • 6
  • 17
1

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. For instance:

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

Xcode will recognize you want multiple images and will play those through in order.

Look at THIS

Community
  • 1
  • 1
iSkore
  • 7,394
  • 3
  • 34
  • 59
0

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,

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.

and by changing the properties of aBar layer u get different effects.

Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
Shankar BS
  • 8,394
  • 6
  • 41
  • 53
0

For this specific gif you may want to use UIActivityIndicatorView and make it animating with startAnimating()

אורי orihpt
  • 2,358
  • 2
  • 16
  • 41