详细内容可参看:http://engineering.flipboard.com/2014/05/animated-gif/
源代码地址:https://github.com/Flipboard/FLAnimatedImage
FLAnimatedImage is a performant animated GIF engine for iOS:
It's a well-tested component that powers all GIFs in Flipboard. To understand its behavior it comes with an interactive demo:
FLAnimatedImage is a well encapsulated drop-in component. Simply replace your UIImageView
instances with instances of FLAnimatedImageView
to get animated GIF support. There is no central cache or state to manage.
To get started, simply copy the two classes FLAnimatedImage.h/.m
and FLAnimatedImageView.h/.m
into your Xcode project or add via CocoaPods by adding this to your Podfile:
pod 'FLAnimatedImage', '~> 1.0'
In your code, #import "FLAnimatedImage.h"
and #import "FLAnimatedImageView.h"
and then create an image from an animated GIF and setup the image view to display it:
FLAnimatedImage *image = [[FLAnimatedImage alloc] initWithAnimatedGIFData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://raphaelschaad.com/static/nyan.gif"]]];
FLAnimatedImageView *imageView = [[FLAnimatedImageView alloc] init];
imageView.animatedImage = image;
imageView.frame = CGRectMake(0.0, 0.0, 100.0, 100.0);
[self.view addSubview:imageView];
It's flexible to integrate in your custom image loading stack and backwards compatible to iOS 5.
It uses ARC and the Apple frameworks QuartzCore
, ImageIO
, MobileCoreServices
, and CoreGraphics
.
Since FLAnimatedImage is licensed under MIT it's compatible with the terms of using it for any app on the App Store.
FLAnimatedImage
should become a UIImage
subclassUIScrollView
supportThis has shipped to many people and since mid 2013 we made many tweaks. But it's a version 1.0, so please come with your questions, issues and pull requests.
Feel free to reach out to @RaphaelSchaad for further help.
大家在运行demo时会卡顿很久,现在引入SDWebImage的cache机制,制作了一个category
此修改只适用于测试,不建议用在app中
源码地址:https://github.com/iunion/FLAnimatedImage
使用方法:
NSURL *url2 = [NSURL URLWithString:@"http://raphaelschaad.com/static/nyan.gif"]; //NSData *data2 = [NSData dataWithContentsOfURL:url2]; //FLAnimatedImage *animatedImage2 = [[FLAnimatedImage alloc] initWithAnimatedGIFData:data2]; //self.imageView2.animatedImage = animatedImage2; __block RootViewController *aself = self; [self.imageView2 setImageWithURL:url2 animatedsuccess:^(UIImage *image, FLAnimatedImage *animatedImage, BOOL cached) { aself.imageView2.debug_delegate = aself.debugView2; animatedImage.debug_delegate = aself.debugView2; aself.debugView2.imageView = aself.imageView2; aself.debugView2.image = animatedImage; aself.imageView2.userInteractionEnabled = YES; } failure:^(NSError *error) { }];