Two ways for iOS to load dynamic graphs
I
View = [[UIView alloc] initWithFrame: CGRectMake (0, 0,100,100)];
NSString * imagePath = [[NSBundle mainBundle] pathForResource: @ "csjg" ofType: @ "gif"];
CGImageSourceRef cImageSource = CGImageSourceCreateWithURL (_ bridge CFURLRef) [NSURL fileURLWithPath: imagePath], NULL );
Size_t imageCount = CGImageSourceGetCount (cImageSource );
NSMutableArray * images = [[NSMutableArray alloc] initWithCapacity: imageCount];
NSMutableArray * times = [[NSMutableArray alloc] initWithCapacity: imageCount];
NSMutableArray * keyTimes = [[NSMutableArray alloc] 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 = [properties valueForKey :( _ bridge NSString *) kCGImagePropertyGIFDictionary];
NSString * gifDelayTime = [gifProperties valueForKey :( _ bridge NSString *) kCGImagePropertyGIFDelayTime];
[Times addObject: gifDelayTime];
TotalTime + = [gifDelayTime floatValue];
// _ 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;
[KeyTimes addObject: [NSNumber numberWithFloat: keyTime];
CurrentTime + = [[times objectAtIndex: I] floatValue];
}
CAKeyframeAnimation * animation = [CAKeyframeAnimation animationWithKeyPath: @ "contents"];
[Animation setTimingFunction: [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionLinear];
[Animation setValues: images];
[Animation setKeyTimes: keyTimes];
Animation. duration = totalTime;
Animation. repeatCount = HUGE_VALF;
[View. layer addAnimation: animation forKey: @ "gifAnimation"];
II
//
// GifView. h
// GIFViewer
//
// Created by xToucher04 on 11-11-9.
// Copyright 2011 Toucher. All rights reserved.
//
# Import
# Import
@ Interface GifView: UIView {
CGImageSourceRef gif;
NSDictionary * gifProperties;
Size_t index;
Size_t count;
Nstmer * 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
@ Implementation GifView
-(Id) initWithFrame :( CGRect) frame filePath :( NSString *) _ filePath {
Self = [super initWithFrame: frame];
If (self ){
GifProperties = [[NSDictionary dictionaryWithObject: [NSDictionary dictionaryWithObject: [NSNumber numberWithInt: 0] forKey :( NSString *) kCGImagePropertyGIFLoopCount]
ForKey :( NSString *) kCGImagePropertyGIFDictionary] retain];
Gif = CGImageSourceCreateWithURL (CFURLRef) [NSURL fileURLWithPath: _ filePath], (CFDictionaryRef) gifProperties );
Count = CGImageSourceGetCount (gif );
Timer = [NSTimer scheduledTimerWithTimeInterval: 0.12 target: self selector: @ selector (play) userInfo: nil repeats: YES];
[Timer fire];
}
Return self;
}
-(Id) initWithFrame :( CGRect) frame data :( NSData *) _ data {
Self = [super initWithFrame: frame];
If (self ){
GifProperties = [[NSDictionary dictionaryWithObject: [NSDictionary dictionaryWithObject: [NSNumber numberWithInt: 0] forKey :( NSString *) kCGImagePropertyGIFLoopCount]
ForKey :( NSString *) kCGImagePropertyGIFDictionary] retain];
// Gif = CGImageSourceCreateWithURL (CFURLRef) [NSURL fileURLWithPath: _ filePath], (CFDictionaryRef) gifProperties );
Gif = CGImageSourceCreateWithData (CFDataRef) _ data, (CFDictionaryRef) gifProperties );
Count = CGImageSourceGetCount (gif );
Timer = [NSTimer scheduledTimerWithTimeInterval: 0.5 target: self selector: @ selector (play) userInfo: nil repeats: YES];
[Timer fire];
}
Return self;
}
-(Void) play
{
Index ++;
Index = index % count;
CGImageRef ref = CGImageSourceCreateImageAtIndex (gif, index, (CFDictionaryRef) gifProperties );
Self. layer. contents = (id) ref;
CFRelease (ref );
}
-(Void) removeFromSuperview
{
NSLog (@ "removeFromSuperview ");
[Timer invalidate];
Timer = nil;
[Super removeFromSuperview];
}
-(Void) dealloc {
NSLog (@ "dealloc ");
CFRelease (gif );
[GifProperties release];
[Super dealloc];
}
@ End
When using:
GifView * pathView = [[GifView alloc] initWithFrame: CGRectMake (100, 0,100,100) filePath: [[NSBundle mainBundle] pathForResource: @ "csjg" ofType: @ "gif"];