如何在ios中加载gif图像并控制它?

时间:2014-08-01 12:54:12

标签: ios xcode startup gif

在我的应用程序中,我想在我的视图中加载gif图像,并控制停止并继续制作动画。when i click pause button application should stop animating and when again click start animating

2 个答案:

答案 0 :(得分:2)

请遵循以下可能对您有帮助的教程。  http://anand3777.blogspot.in/2014/07/gif-image-loading.html

第1步:

   Download library from below url and add it on your project.

https://drive.google.com/folderview?id=0B5JC34Lt79ctamtOVTV6SHhzOVU&usp=sharing

第2步:

  Import the file on your "viewController.h" like below.
  //gifImage//
  #import "SCGIFImageView.h"
 //gifImage//

第3步:

    Create object like given below on your "viewController.h".
    //gifImage//
    IBOutlet SCGIFImageView* _gifImageView;
    IBOutlet UIButton* _button;
    //gifImage//

第4步:

   Create ibaction on your "viewController".
   //gifImage//
    - (IBAction)controlAnimate:(id)sender;
   //gifImage//

第5步:

   Add the following code were you want to use gif image
  //gifImage//
  //load url gif image
  NSURL *imageURL = [NSURL URLWithString:@"http://google.co.in/anim.gif"];
  NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
  /*
   //load local gif image
   NSString* filePath = [[NSBundle mainBundle] pathForResource:@"1.gif" ofType:nil];
   NSData* imageData = [NSData dataWithContentsOfFile:filePath];
 */
  _gifImageView = [[SCGIFImageView alloc] initWithFrame:CGRectMake(225, 70, 75, 75)] ;
 [_gifImageView setData:imageData];
 [self.view addSubview:_gifImageView];
 //gifImage//

第6步:

   Add the following code for stop/start animating
  //gifImage//
  - (IBAction)controlAnimate:(id)sender{
     _gifImageView.animating = !_gifImageView.animating;

     if (_gifImageView.animating) {
         [_button setTitle:@"Pause" forState:UIControlStateNormal];
     } else {
         [_button setTitle:@"Continue" forState:UIControlStateNormal];
     }
  }
 //gifImage//


[1]: https://drive.google.com/folderview?id=0B5JC34Lt79ctamtOVTV6SHhzOVU&usp=sharing

答案 1 :(得分:0)

使用https://github.com/Flipboard/FLAnimatedImage中的最新GIF课程 它易于使用且对内存友好。

在您的代码#import "FLAnimatedImage.h"中,使用动画GIF创建图片,然后设置图片视图以显示它:

FLAnimatedImage *image         = [FLAnimatedImage animatedImageWithGIFData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://upload.wikimedia.org/wikipedia/commons/2/2c/Rotating_earth_%28large%29.gif"]]];
FLAnimatedImageView *imageView = [[FLAnimatedImageView alloc] init];
imageView.animatedImage        = image;
imageView.frame                = CGRectMake(0.0, 0.0, 100.0, 100.0); //As your Wish you can set frame
[self.view addSubview:imageView];  

或者如果您使用的是Bundle gif文件,那么

NSURL *url                     = [[NSBundle mainBundle] URLForResource:@"loading_01" withExtension:@"gif"];

FLAnimatedImage *image         = [FLAnimatedImage animatedImageWithGIFData:[NSData dataWithContentsOfURL:url]];
FLAnimatedImageView *imageView = [[FLAnimatedImageView alloc] init];
imageView.animatedImage        = image;
imageView.frame                = CGRectMake(0.0, 0.0, 100.0, 100.0); //As your Wish you can set frame
[self.view addSubview:imageView];
相关问题