0

I want to show an animated ".gif" in a ImageView. Now I managed it like here: click with UIImage+animatedGIF. But if it begins to animate 2 gifs it uses 45Mb of memory I think that is very much for 2 gif's with 100kb. What can I do? Code to start animation here:

NSString *path = [[NSBundle mainBundle]pathForResource:@"animation" ofType:@"gif"];             
NSURL *url = [[NSURL alloc] initFileURLWithPath:path];         
_testImage.image = [UIImage animatedImageWithAnimatedGIFURL:url];         
_test1.image = [UIImage animatedImageWithAnimatedGIFURL:url]; 
Community
  • 1
  • 1
user3195648
  • 67
  • 1
  • 1
  • 6
  • Can you post some of the relevant code which you are using to animate the gifs? – 67cherries May 13 '14 at 19:41
  • it's too long I got it from github here is the link: https://github.com/mayoff/uiimage-from-animated-gif/blob/master/uiimage-from-animated-gif/UIImage%2BanimatedGIF.m – user3195648 May 13 '14 at 19:43
  • do you think I should import every single picture from the gif to Supporting Files folder and should change pictures via code? – user3195648 May 13 '14 at 19:52
  • Can you post the code in your app where you create the UIImageView and give it the data for the gif? Also, how do you know how much memory the image is using? – 67cherries May 13 '14 at 19:53
  • Because the animation starts when you click on the UIImageView. So the UIImageView is created via Storyboard and it only starts animating with code above later. `NSString *path = [[NSBundle mainBundle]pathForResource:@"animation" ofType:@"gif"]; NSURL *url = [[NSURL alloc] initFileURLWithPath:path]; _testImage.image = [UIImage animatedImageWithAnimatedGIFURL:url]; _test1.image = [UIImage animatedImageWithAnimatedGIFURL:url]; ` – user3195648 May 13 '14 at 20:02
  • And if I stop animating so if I only do `_testImage.image = [UIImage imageNamed:@"animation.gif"];` the memory usage gets back to 11Mb – user3195648 May 13 '14 at 20:09
  • post a proper question, do not post the content related to your question in comment. update your question with the content you have posted here in comment for better understanding. good luck – Pawan Rai May 13 '14 at 20:13
  • I still do not understand how you know that the memory usage goes up. Could you please clarify? – 67cherries May 13 '14 at 20:22
  • Yes Instrument show's it for simulator. And on my iPhone I can see it via a tweak. – user3195648 May 13 '14 at 20:25

2 Answers2

6

I had same issue while adding animating images on UIScrollView. Use FLAnimatedImageView. https://github.com/Flipboard/FLAnimatedImage

IT works like magic. :)

Ashutosh Dubey
  • 385
  • 1
  • 4
  • 14
5

Try this

    // Load images
    NSArray *imageNames = @[@"win_1.png", @"win_2.png", @"win_3.png", @"win_4.png",
                        @"win_5.png", @"win_6.png", @"win_7.png", @"win_8.png",
                        @"win_9.png", @"win_10.png", @"win_11.png", @"win_12.png",
                        @"win_13.png", @"win_14.png", @"win_15.png", @"win_16.png"];

    NSMutableArray *images = [[NSMutableArray alloc] init];
    for (int i = 0; i < imageNames.count; i++) 
    {
        [images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
    }

    // Normal Animation
    UIImageView *animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 95, 86, 193)];
    animationImageView.animationImages = images;
    animationImageView.animationDuration = 0.5;

    [self.view addSubview:animationImageView];
    [animationImageView startAnimating];
Deepak
  • 1,421
  • 1
  • 16
  • 20