c语言显示gif图片,iOS开发中实现显示gif图片的方法

//

//  SvGifView.h

//  SvGifSample

//

//  Created by maple on 3/28/13.

//  Copyright (c) 2013 smileEvday. All rights reserved.

//

#import

@interface SvGifView : UIView

/*

* @brief desingated initializer

*/

- (id)initWithCenter:(CGPoint)center fileURL:(NSURL*)fileURL;

/*

* @brief start Gif Animation

*/

- (void)startGif;

/*

* @brief stop Gif Animation

*/

- (void)stopGif;

/*

* @brief get frames image(CGImageRef) in Gif

*/

+ (NSArray*)framesInGif:(NSURL*)fileURL;

@end

//

//  SvGifView.m

//  SvGifSample

//

//  Created by maple on 3/28/13.

//  Copyright (c) 2013 smileEvday. All rights reserved.

//

#import "SvGifView.h"

#import

#import

/*

* @brief resolving gif information

*/

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:(id)frame];

CGImageRelease(frame);

// get gif info with each frame

NSDictionary *dict = (NSDictionary*)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];

}

// kCGImagePropertyGIFDictionary中kCGImagePropertyGIFDelayTime,kCGImagePropertyGIFUnclampedDelayTime值是一样的

NSDictionary *gifDict = [dict valueForKey:(NSString*)kCGImagePropertyGIFDictionary];

[delayTimes addObject:[gifDict valueForKey:(NSString*)kCGImagePropertyGIFDelayTime]];

if (totalTime) {

*totalTime = *totalTime + [[gifDict valueForKey:(NSString*)kCGImagePropertyGIFDelayTime] floatValue];

}

}

}

@interface SvGifView() {

NSMutableArray *_frames;

NSMutableArray *_frameDelayTimes;

CGFloat _totalTime;         // seconds

CGFloat _width;

CGFloat _height;

}

@end

@implementation SvGifView

- (id)initWithCenter:(CGPoint)center fileURL:(NSURL*)fileURL;

{

self = [super initWithFrame:CGRectZero];

if (self) {

_frames = [[NSMutableArray alloc] init];

_frameDelayTimes = [[NSMutableArray alloc] init];

_width = 0;

_height = 0;

if (fileURL) {

getFrameInfo((CFURLRef)fileURL, _frames, _frameDelayTimes, &_totalTime, &_width, &_height);

}

self.frame = CGRectMake(0, 0, _width, _height);

self.center = center;

}

return self;

}

+ (NSArray*)framesInGif:(NSURL *)fileURL

{

NSMutableArray *frames = [NSMutableArray arrayWithCapacity:3];

NSMutableArray *delays = [NSMutableArray arrayWithCapacity:3];

getFrameInfo((CFURLRef)fileURL, frames, delays, NULL, NULL, NULL);

return frames;

}

- (void)startGif

{

CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];

NSMutableArray *times = [NSMutableArray arrayWithCapacity: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 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 = 5;

[self.layer addAnimation:animation forKey:@"gifAnimation"];

}

- (void)stopGif

{

[self.layer removeAllAnimations];

}

// remove contents when animation end

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag

{

self.layer.contents = nil;

}

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect

{

// Drawing code

}

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值