【发布时间】:2014-09-24 16:31:24
【问题描述】:
在我的应用程序中,我想在我的视图中加载 gif 图像并控制停止和继续动画。
【问题讨论】:
在我的应用程序中,我想在我的视图中加载 gif 图像并控制停止和继续动画。
【问题讨论】:
请遵循以下可能对您有所帮助的教程。 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
【讨论】:
使用来自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];
【讨论】: