IOS Play GIF animations

Source: Internet
Author: User

//

LoadGifView.h

Playgif

//

Created by cold bamboo on 15/4/27.

Copyright (c) 2015 cold bamboo. All rights reserved.

//

#import <UIKit/UIKit.h>

@interface Loadgifview:uiview

/**

* @brief Initialization

*

* @return

*/

-(Instancetype) initWithFrame: (CGRect) frame localfileurl: (Nsurl *) FileURL;

/**

* @brief start GIF animation

*

* @return

*/

-(void) startgifanimation;

/**

* @brief Stop GIF animation

*

* @return

*/

-(void) stopgifanimation;

/**

* @brief Get the frame of all the pictures in GIF

*

* @return

*/

+ (Nsarray *) Getframesfromgif: (Nsurl *) FileURL;

@end

//

loadgifview.m

Playgif

//

Created by cold bamboo on 15/4/27.

Copyright (c) 2015 cold bamboo. All rights reserved.

Play GIF

#import "LoadGifView.h"

#import <ImageIO/ImageIO.h>

#import <QuartzCore/CoreAnimation.h>

@interface Loadgifview ()

@property (nonatomic, strong) Nsmutablearray * frames;

@property (nonatomic, Strong) Nsmutablearray * framedelaytimes;

@property (nonatomic, assign) CGFloat totaltime;

@property (nonatomic, assign) cgfloat width;

@property (nonatomic, assign) cgfloat height;

@end

@implementation Loadgifview

/**

* @brief get information on GIF images

*

* @param

* @return

*/

void Getframeinfo (cfurlref url, nsmutablearray *frames, Nsmutablearray *delaytimes, cgfloat *totaltime,cgfloat * Gifwidth, CGFloat *gifheight)

{

Cgimagesourceref gifsource = Cgimagesourcecreatewithurl (URL, NULL);

Get frame Count

size_t Framecount = Cgimagesourcegetcount (Gifsource);

for (size_t i = 0; i < Framecount; ++i) {

Get each frame

Cgimageref frame = Cgimagesourcecreateimageatindex (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 Kcgimagepropertygifdelaytime,kcgimagepropertygifunclampeddelaytime value in Kcgimagepropertygifdictionary is the same

Nsdictionary *gifdict = [dict valueforkey: (nsstring*) kcgimagepropertygifdictionary];

[Delaytimes addobject:[gifdict Valueforkey: (nsstring*) kcgimagepropertygifdelaytime]];

if (totaltime) {

*totaltime = *totaltime + [[gifdict Valueforkey: (nsstring*) kcgimagepropertygifdelaytime] floatValue];

}

Cfrelease (Cfbridgingretain (dict));

}

if (Gifsource) {

Cfrelease (Gifsource);

}

}

-(Instancetype) initWithFrame: (CGRect) frame localfileurl: (Nsurl *) FileURL

{

self = [super Initwithframe:frame];

if (self) {

_frames = [[Nsmutablearray alloc] init];

_framedelaytimes = [[Nsmutablearray alloc] init];

_width = 0;

_height = 0;

if (FileURL) {

Getframeinfo (__bridge cfurlref) FileURL, _frames, _framedelaytimes, &_totaltime, &_width, &_height);

}

}

return self;

}

+ (Nsarray *) Getframesfromgif: (Nsurl *) FileURL

{

Nsmutablearray *frames = [Nsmutablearray arraywithcapacity:3];

Nsmutablearray *delays = [Nsmutablearray arraywithcapacity:3];

Getframeinfo ((__bridge cfurlref) FileURL, frames, delays, NULL, NULL, NULL);

return frames;

}

-(void) startgifanimation

{

Cakeyframeanimation *animation = [cakeyframeanimation animationwithkeypath:@ "Contents"];

Nsmutablearray *times = [Nsmutablearray arraywithcapacity:3];

CGFloat currenttime = 0;

int count = (int) _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 arraywithcapacity:3];

for (int i = 0; i < count; ++i) {

[Images Addobject:[_frames objectatindex:i];

}

[Animation setvalues:images];

[Animation settimingfunction:[camediatimingfunction functionwithname:kcamediatimingfunctionlinear];

Animation.duration = _totaltime;

Animation.delegate = self;

Animation.repeatcount = maxfloat;

[Self.layer addanimation:animation forkey:@ "Gifanimation"];

}

-(void) stopgifanimation

{

[Self.layer removeallanimations];

}

@end

//

PlayGifViewController.h

Playgif

//

Created by cold bamboo on 15/4/27.

Copyright (c) 2015 cold bamboo. All rights reserved.

//

#import <UIKit/UIKit.h>

@interface Playgifviewcontroller:uiviewcontroller

@end

//

Playgifviewcontroller.m

Playgif

//

Created by cold bamboo on 15/4/27.

Copyright (c) 2015 cold bamboo. All rights reserved.

//

#import "PlayGifViewController.h"

#import "LoadGifView.h"

@interface Playgifviewcontroller ()

@property (nonatomic, Strong) Nsarray * IMAGEARR;

@end

@implementation Playgifviewcontroller

-(void) Viewdidload {

[Super Viewdidload];

Self.view.backgroundColor = [Uicolor blackcolor];

Play GIF animations

NSString * Path = [[NSBundle mainbundle] pathforresource:@ "2.gif" oftype:nil];

Loadgifview * Gifview = [[Loadgifview alloc] Initwithframe:cgrectmake ([UIScreen mainscreen].bounds.size.width-200)/2 .0f, Localfileurl:[nsurl Fileurlwithpath:path]];

[Gifview startgifanimation];

[Self.view Addsubview:gifview];

}

-(void) didreceivememorywarning {

[Super didreceivememorywarning];

}

@end

IOS Play GIF animations

Related Article

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.