iOS加载动态图的两种方法

alloc] initWithFrame:CGRectMake(0,,100)];

mainBundle] pathForResource:@"csjg"ofType

CGImageSourceRef cImageSource = :imagePath], NULL);

size_t imageCount = CGImageSourceGetCount(cImageSource);

NSMutableArray *images = [[NSMutableArrayalloc] initWithCapacity:imageCount];

NSMutableArray *times = [[NSMutableArrayalloc] initWithCapacity:imageCount];

NSMutableArray *keyTimes = [[NSMutableArrayalloc] initWithCapacity:imageCount];

float totalTime = 0;

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

CGImageRef cgimage= CGImageSourceCreateImageAtIndex(cImageSource, i,NULL);

[images addObject:(__bridge id)cgimage];

CGImageRelease(cgimage);

NSDictionary *properties = (__bridge NSDictionary *)CGImageSourceCopyPropertiesAtIndex(cImageSource, i,NULL);

NSDictionary *gifProperties = [propertiesNSString *)kCGImagePropertyGIFDictionary];

NSString *gifDelayTime = [gifProperties valueForKey:(__bridgeNSString* )kCGImagePropertyGIFDelayTime];

[times addObject:gifDelayTime];

totalTime += [gifDelayTimefloatValue];

// _size.width = [[properties valueForKey:(NSString*)kCGImagePropertyPixelWidth] floatValue];

// _size.height = [[properties valueForKey:(NSString*)kCGImagePropertyPixelHeight] floatValue];

}

float currentTime = 0;

for (size_t i = 0; i < times.count; i++) {

float keyTime = currentTime / totalTime;

[keyTimesaddObject:[NSNumbernumberWithFloat:keyTime]];

currentTime += [[timesobjectAtIndex:i] floatValue];

}

CAKeyframeAnimation *animation = [CAKeyframeAnimation

CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionLinear]];

[animation setValues:images];

[animation setKeyTimes:keyTimes];

animation.duration = totalTime;

animation.repeatCount =HUGE_VALF;

[view.layeraddAnimation:animation forKey:@"gifAnimation"];

//

// GifView.h

// GIFViewer

//

// Created by xToucher04 on 11-11-9.

// Copyright 2011 Toucher. All rights reserved.

//

#import <UIKit/UIKit.h>

#import <ImageIO/ImageIO.h>

@interface GifView :UIView {

CGImageSourceRef gif;

NSDictionary *gifProperties;

size_t index;

size_t count;

NSTimer *timer;

}

– (id)initWithFrame:(CGRect)frame filePath:(NSString *)_filePath;

– (id)initWithFrame:(CGRect)frame data:(NSData *)_data;

@end

//

// GifView.m

// GIFViewer

//

// Created by xToucher04 on 11-11-9.

// Copyright 2011 Toucher. All rights reserved.

//

#import "GifView.h"

#import <QuartzCore/QuartzCore.h>

@implementation GifView

– (id)initWithFrame:(CGRect)frame filePath:(NSString *)_filePath{

self = [super initWithFrame:frame];

if (self) {

NSDictionaryNSNumber]forKey:(NSString *)kCGImagePropertyGIFLoopCount]

NSString *)kCGImagePropertyGIFDictionary]retain];

gif =CGImageSourceCreateWithURLfileURLWithPath

CGImageSourceGetCount

scheduledTimerWithTimeIntervalselector:@selector(play)userInfo:nilrepeats

[timerfire];

}

returnself;

}

– (id)initWithFrame:(CGRect)frame data:(NSData *)_data{

self = [super initWithFrame:frame];

if (self) {

NSDictionaryNSNumber]forKey:(NSString *)kCGImagePropertyGIFLoopCount]

NSString *)kCGImagePropertyGIFDictionary]retain];

//gif = CGImageSourceCreateWithURL((CFURLRef)[NSURL fileURLWithPath:_filePath], (CFDictionaryRef)gifProperties);

gif =CGImageSourceCreateWithData

CGImageSourceGetCount

scheduledTimerWithTimeIntervalselector:@selector(play)userInfo:nilrepeats

[timerfire];

}

returnself;

}

-(void)play

{

index ++;

index =index%count;

CGImageRef ref =CGImageSourceCreateImageAtIndex(gif,

layer

CFRelease(ref);

}

-(void)removeFromSuperview

{

@"removeFromSuperview");

[timerinvalidate];

timer =nil;

[superremoveFromSuperview];

}

– (void)dealloc {

@"dealloc");

[gifPropertiesrelease];

[superdealloc];

}

@end

使用的时候:

allocCGRectMake(100,,mainBundle] pathForResource:@"csjg"ofType

,分之百的把自己推销给自己。

iOS加载动态图的两种方法

相关文章:

你感兴趣的文章:

标签云: