IOS GIF and iosgif

Source: Internet
Author: User

IOS GIF and iosgif

Here we will record the code that enables GIF animation playback on iOS.


Call code:

@interface ViewController ()@property (nonatomic , strong) QGifView *gifView;@end@implementation ViewController- (void) viewDidAppear:(BOOL)animated{    [super viewDidAppear:animated];    [_gifView startGif];}- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.        NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"snow" withExtension:@"gif"];        _gifView = [[QGifView alloc] initWithCenter:CGPointMake(self.view.bounds.size.width / 2, 13) fileURL:fileUrl];        _gifView.backgroundColor = [UIColor clearColor];        _gifView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;            [self.view addSubview:_gifView];        UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];    btn.frame = CGRectMake(0, 0, 100, 60);    btn.center = CGPointMake(100, self.view.bounds.size.height - 50);    btn.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;    [btn setTitle:@"Start Gif" forState:UIControlStateNormal];    [btn addTarget:self action:@selector(startGif) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:btn];            UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];    btn2.frame = CGRectMake(0, 0, 100, 60);    btn2.center = CGPointMake(220, self.view.bounds.size.height - 50);    btn2.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;    [btn2 setTitle:@"Stop Gif" forState:UIControlStateNormal];    [btn2 addTarget:self action:@selector(stopGif) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:btn2];    }- (void)startGif{    [_gifView startGif];}- (void)stopGif{    [_gifView stopGif];}


GIf playback View implementation

QGifView. h

#import <UIKit/UIKit.h>@interface QGifView : UIView- (id) initWithCenter:(CGPoint) center fileURL:(NSURL *)fileURL;- (void) startGif;- (void) stopGif;@end

QGifView. m

# Import "QGifView. h "# import <ImageIO/ImageIO. h> # import <QuartzCore/QuartzCore. h> @ interface QGifView () @ property (nonatomic, strong) NSMutableArray * frames; @ property (nonatomic, strong) limit * frameDelayTimes; @ property (nonatomic, assign) CGFloat totalTime; @ property (nonatomic, assign) CGFloat width; @ property (nonatomic, assign) CGFloat height; @ end @ implementation QGifView/** @ brief res Olving gif information */void getFrameInfo (CFURLRef url, comment * frames, comment * delayTimes, CGFloat * totalTime, CGFloat * gifWidth, CGFloat * gifHeight) {CGImageSourceRef gifSource = Response (url, NULL); // get frame count size_t frameCount = CGImageSourceGetCount (gifSource); for (size_t I = 0; I <frameCount; ++ I) {// get each frame CGImageRef frame = CGImageSourc ECreateImageAtIndex (gifSource, I, NULL); [frames addObject :( _ bridge id) frame]; CGImageRelease (frame ); // get gif info with each frame NSDictionary * dict = (NSDictionary *) CFBridgingRelease (CGImageSourceCopyPropertiesAtIndex (gifSource, I, NULL); NSLog (@ "kCGImagePropertyGIFDictionary % @", [dict valueForKey :( NSString *) kCGImagePropertyGIFDictionary]); // get gif size if (gifWidth! = NULL & gifHeight! = NULL) {* gifWidth = [[dict valueForKey :( NSString *) kCGImagePropertyPixelWidth] floatValue]; * gifHeight = [[dict valueForKey :( NSString *) kCGImagePropertyPixelHeight] floatValue];} // The region in the region. The region value is the same as NSDictionary * gifDict = [dict valueForKey :( NSString *) region]; [delayTimes addObject: [gifDict valueForKey :( NSString *) region]; if (totalTime) {* totalTime = * totalTime + [[gifDict valueForKey :( NSString *) kCGImagePropertyGIFDelayTime] floatValue] ;}}-(id) initWithCenter :( CGPoint) center fileURL :( NSURL *) fileURL {if (self = [super initWithFrame: CGRectZero]) {_ frames = [NSMutableArray array]; _ frameDelayTimes = [NSMutableArray array]; _ width = 0; _ height = 0; if (fileURL) {getFrameInfo (_ bridge CFURLRef) fileURL, _ frames, _ frameDelayTimes, & _ totalTime, & _ width, & _ height);} self. frame = CGRectMake (0, 0, _ width, _ height); self. center = center;} return self;}-(void) startGif {CAKeyframeAnimation * animation = [CAKeyframeAnimation usage: @ "contents"]; NSMutableArray * times = [NSMutableArray usage: 3]; CGFloat currentTime = 0; int count = _ frameDelayTimes. count; for (int I = 0; I <count; ++ I) {[times addObject: [NSNumber numberWithFloat :( currentTime/_ totalTime)]; currentTime + = [[_ frameDelayTimes objectAtIndex: I] floatValue];} [animation setKeyTimes: times]; NSMutableArray * images = [NSMutableArray Syntax: 3]; for (int I = 0; I <count; ++ I) {[images addObject: [_ frames objectAtIndex: I];} [animation setValues: images]; [animation setTimingFunction: [edicamatimingfunction functionWithName: kCAMediaTimingFunctionLinear]; animation. duration = _ totalTime; animation. delegate = self; animation. repeatCount = 50; [self. layer addAnimation: animation forKey: @ "gifAnimation"];}-(void) animationDidStop :( CAAnimation *) anim finished :( BOOL) flag {self. layer. contents = nil;}-(void) stopGif {[self. layer removeAllAnimations];} @ end

Code download: http://download.csdn.net/detail/qqmcy/8502055


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.