Two ways for iOS to load dynamic graphs

Source: Internet
Author: User

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"];



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.