ios 展示gif动画
#import "SCGIFImageView.h"
NSString* filePath = [[nsbundle mainbundle] pathForResource:@"1.gif" ofType:nil];
SCGIFImageView* gifImageView = [[[scgifimageview alloc] initWithGIFFile:filePath] autorelease];
gifImageView.frame = CGRectMake(0, 0, gifImageView.image.size.width, gifImageView.image.size.height);
gifImageView.center = self.view.center;
[self.view addsubview:gifimageview];
SCGIFImageView.h
//
// SCGIFImageView.h
// TestGIF
//
// Created by shichangone on 11-7-12.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface AnimatedGifFrame : NSObject
{
NSData *data;
NSData *header;
double delay;
int disposalMethod;
CGRect area;
}
@property (nonatomic, copy) NSData *header;
@property (nonatomic, copy) NSData *data;
@property (nonatomic) double delay;
@property (nonatomic) int disposalMethod;
@property (nonatomic) CGRect area;
@end
@interface SCGIFImageView : UIImageView {
NSData *GIF_pointer;
NSMutableData *GIF_buffer;
NSMutableData *GIF_screen;
NSMutableData *GIF_global;
NSMutableArray *GIF_frames;
int GIF_sorted;
int GIF_colorS;
int GIF_colorC;
int GIF_colorF;
int animatedGifDelay;
int dataPointer;
}
@property (nonatomic, retain) NSMutableArray *GIF_frames;
- (id)initWithGIFFile:(NSString*)gifFilePath;
- (id)initWithGIFData:(NSData*)gifImageData;
- (void)loadImageData;
+ (NSMutableArray*)getGifFrames:(NSData*)gifImageData;
+ (BOOL)isGifImage:(NSData*)imageData;
- (void) decodeGIF:(NSData *)GIFData;
- (void) GIFReadExtensions;
- (void) GIFReadDescriptor;
- (bool) GIFGetBytes:(int)length;
- (bool) GIFSkipBytes: (int) length;
- (NSData*) getFrameAsDataAtIndex:(int)index;
- (UIImage*) getFrameAsImageAtIndex:(int)index;
@end
SCGIFImageView.m
//
// SCGIFImageView.m
// TestGIF
//
// Created by shichangone on 11-7-12.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "SCGIFImageView.h"
@implementation AnimatedGifFrame
@synthesize data, delay, disposalMethod, area, header;
- (void) dealloc
{
[data release];
[header release];
[super dealloc];
}
@end
@implementation SCGIFImageView
@synthesize GIF_frames;
+ (BOOL)isGifImage:(NSData*)imageData {
const char* buf = (const char*)[imagedata bytes];
if (buf[0] == 0x47 && buf[1] == 0x49 && buf[2] == 0x46 && buf[3] == 0x38) {
return YES;
}
return NO;
}
+ (NSMutableArray*)getGifFrames:(NSData*)gifImageData {
SCGIFImageView* gifImageView = [[scgifimageview alloc] initWithGIFData:gifImageData];
if (!gifImageView) {
return nil;
}
NSMutableArray* gifFrames = gifImageView.GIF_frames;
[[gifframes retain] autorelease];
[gifimageview release];
return gifFrames;
}
- (id)initWithGIFFile:(NSString*)gifFilePath {
NSData* imageData = [nsdata datawithcontentsoffile:giffilepath];
return [self initwithgifdata:imagedata];
}
- (id)initWithGIFData:(NSData*)gifImageData {
if (gifImageData.length < 4) {
return nil;
}
if (![scgifimageview isgifimage:gifimagedata]) {
UIImage* image = [uiimage imagewithdata:gifimagedata];
return [super initwithimage:image];
}
[self decodegif:gifimagedata];
if (GIF_frames.count <= 0) {
UIImage* image = [uiimage imagewithdata:gifimagedata];
return [super initwithimage:image];
}
self = [super init];
if (self) {
[self loadimagedata];
}
return self;
}
- (void)setGIF_frames:(NSMutableArray *)gifFrames {
[gifframes retain];
if (GIF_frames) {
[gif_frames release];
}
GIF_frames = gifFrames;
[self loadimagedata];
}
- (void)loadImageData {
// Add all subframes to the animation
NSMutableArray *array = [[nsmutablearray alloc] init];
for (NSUInteger i = 0; i < [gif_frames count]; i++)
{
[array addobject: [self getframeasimageatindex:i]];
}
NSMutableArray *overlayArray = [[nsmutablearray alloc] init];
UIImage *firstImage = [array objectatindex:0];
CGSize size = firstImage.size;
CGRect rect = CGRectZero;
rect.size = size;
UIGraphicsBeginImageContext(size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
int i = 0;
AnimatedGifFrame *lastFrame = nil;
for (UIImage *image in array)
{
// Get Frame
AnimatedGifFrame *frame = [gif_frames objectatindex:i];
// Initialize Flag
UIImage *previousCanvas = nil;
// Save Context
CGContextSaveGState(ctx);
// Change CTM
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextTranslateCTM(ctx, 0.0, -size.height);
// Check if lastFrame exists
CGRect clipRect;
// Disposal Method (Operations before draw frame)
switch (frame.disposalMethod)
{
case 1: // Do not dispose (draw over context)
// Create Rect (y inverted) to clipping
clipRect = CGRectMake(frame.area.origin.x, size.height - frame.area.size.height - frame.area.origin.y, frame.area.size.width, frame.area.size.height);
// Clip Context
CGContextClipToRect(ctx, clipRect);
break;
case 2: // Restore to background the rect when the actual frame will go to be drawed
// Create Rect (y inverted) to clipping
clipRect = CGRectMake(frame.area.origin.x, size.height - frame.area.size.height - frame.area.origin.y, frame.area.size.width, frame.area.size.height);
// Clip Context
CGContextClipToRect(ctx, clipRect);
break;
case 3: // Restore to Previous
// Get Canvas
previousCanvas = UIGraphicsGetImageFromCurrentImageContext();
// Create Rect (y inverted) to clipping
clipRect = CGRectMake(frame.area.origin.x, size.height - frame.area.size.height - frame.area.origin.y, frame.area.size.width, frame.area.size.height);
// Clip Context
CGContextClipToRect(ctx, clipRect);
break;
}
// Draw Actual Frame
CGContextDrawImage(ctx, rect, image.CGImage);
// Restore State
CGContextRestoreGState(ctx);
//delay must larger than 0, the minimum delay in firefox is 10.
if (frame.delay <= 0) {
frame.delay = 10;
}
[overlayarray addobject:uigraphicsgetimagefromcurrentimagecontext()];
// Set Last Frame
lastFrame = frame;
// Disposal Method (Operations afte draw frame)
switch (frame.disposalMethod)
{
case 2: // Restore to background color the zone of the actual frame
// Save Context
CGContextSaveGState(ctx);
// Change CTM
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextTranslateCTM(ctx, 0.0, -size.height);
// Clear Context
CGContextClearRect(ctx, clipRect);
// Restore Context
CGContextRestoreGState(ctx);
break;
case 3: // Restore to Previous Canvas
// Save Context
CGContextSaveGState(ctx);
// Change CTM
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextTranslateCTM(ctx, 0.0, -size.height);
// Clear Context
CGContextClearRect(ctx, lastFrame.area);
// Draw previous frame
CGContextDrawImage(ctx, rect, previousCanvas.CGImage);
// Restore State
CGContextRestoreGState(ctx);
break;
}
// Increment counter
i++;
}
UIGraphicsEndImageContext();
[self setimage:[overlayarray objectatindex:0]];
[self setanimationimages:overlayarray];
[overlayarray release];
[array release];
// Count up the total delay, since Cocoa doesn't do per frame delays.
double total = 0;
for (AnimatedGifFrame *frame in GIF_frames) {
total += frame.delay;
}
// GIFs store the delays as 1/100th of a second,
// UIImageViews want it in seconds.
[self setanimationduration:total/100];
// Repeat infinite
[self setanimationrepeatcount:0];
[self startanimating];
}
- (void)dealloc {
if (GIF_buffer != nil)
{
[gif_buffer release];
}
if (GIF_screen != nil)
{
[gif_screen release];
}
if (GIF_global != nil)
{
[gif_global release];
}
[gif_frames release];
[super dealloc];
}
- (void) decodeGIF:(NSData *)GIFData {
GIF_pointer = GIFData;
if (GIF_buffer != nil)
{
[gif_buffer release];
}
if (GIF_global != nil)
{
[gif_global release];
}
if (GIF_screen != nil)
{
[gif_screen release];
}
[gif_frames release];
GIF_buffer = [[nsmutabledata alloc] init];
GIF_global = [[nsmutabledata alloc] init];
GIF_screen = [[nsmutabledata alloc] init];
GIF_frames = [[nsmutablearray alloc] init];
// Reset file counters to 0
dataPointer = 0;
[self gifskipbytes: 6]; // GIF89a, throw away
[self gifgetbytes: 7]; // Logical Screen Descriptor
// Deep copy
[gif_screen setdata: gif_buffer];
// Copy the read bytes into a local buffer on the stack
// For easy byte access in the following lines.
int length = [gif_buffer length];
unsigned char aBuffer[length];
[gif_buffer getbytes:abuffer length:length];
if (aBuffer[4] & 0x80) GIF_colorF = 1; else GIF_colorF = 0;
if (aBuffer[4] & 0x08) GIF_sorted = 1; else GIF_sorted = 0;
GIF_colorC = (aBuffer[4] & 0x07);
GIF_colorS = 2 << GIF_colorC;
if (GIF_colorF == 1)
{
[self gifgetbytes: (3 * gif_colors)];
// Deep copy
[gif_global setdata:gif_buffer];
}
unsigned char bBuffer[1];
while ([self gifgetbytes:1] == YES)
{
[gif_buffer getbytes:bbuffer length:1];
if (bBuffer[0] == 0x3B)
{ // This is the end
break;
}
switch (bBuffer[0])
{
case 0x21:
// Graphic Control Extension (#n of n)
[self gifreadextensions];
break;
case 0x2C:
// Image Descriptor (#n of n)
[self gifreaddescriptor];
break;
}
}
// clean up stuff
[gif_buffer release];
GIF_buffer = nil;
[gif_screen release];
GIF_screen = nil;
[gif_global release];
GIF_global = nil;
}
- (void) GIFReadExtensions {
// 21! But we still could have an Application Extension,
// so we want to check for the full signature.
unsigned char cur[1], prev[1];
[self gifgetbytes:1];
[gif_buffer getbytes:cur length:1];
while (cur[0] != 0x00)
{
// TODO: Known bug, the sequence F9 04 could occur in the Application Extension, we
// should check whether this combo follows directly after the 21.
if (cur[0] == 0x04 && prev[0] == 0xF9)
{
[self gifgetbytes:5];
AnimatedGifFrame *frame = [[animatedgifframe alloc] init];
unsigned char buffer[5];
[gif_buffer getbytes:buffer length:5];
frame.disposalMethod = (buffer[0] & 0x1c) >> 2;
//NSLog(@"flags=%x, dm=%x", (int)(buffer[0]), frame.disposalMethod);
// We save the delays for easy access.
frame.delay = (buffer[1] | buffer[2] << 8);
unsigned char board[8];
board[0] = 0x21;
board[1] = 0xF9;
board[2] = 0x04;
for(int i = 3, a = 0; a < 5; i++, a++)
{
board[i] = buffer[a];
}
frame.header = [nsdata datawithbytes:board length:8];
[gif_frames addobject:frame];
[frame release];
break;
}
prev[0] = cur[0];
[self gifgetbytes:1];
[gif_buffer getbytes:cur length:1];
}
}
- (void) GIFReadDescriptor {
[self gifgetbytes:9];
// Deep copy
NSMutableData *GIF_screenTmp = [nsmutabledata datawithdata:gif_buffer];
unsigned char aBuffer[9];
[gif_buffer getbytes:abuffer length:9];
CGRect rect;
rect.origin.x = ((int)aBuffer[1] << 8) | aBuffer[0];
rect.origin.y = ((int)aBuffer[3] << 8) | aBuffer[2];
rect.size.width = ((int)aBuffer[5] << 8) | aBuffer[4];
rect.size.height = ((int)aBuffer[7] << 8) | aBuffer[6];
AnimatedGifFrame *frame = [gif_frames lastobject];
frame.area = rect;
if (aBuffer[8] & 0x80) GIF_colorF = 1; else GIF_colorF = 0;
unsigned char GIF_code = GIF_colorC, GIF_sort = GIF_sorted;
if (GIF_colorF == 1)
{
GIF_code = (aBuffer[8] & 0x07);
if (aBuffer[8] & 0x20)
{
GIF_sort = 1;
}
else
{
GIF_sort = 0;
}
}
int GIF_size = (2 << GIF_code);
size_t blength = [gif_screen length];
unsigned char bBuffer[blength];
[gif_screen getbytes:bbuffer length:blength];
bBuffer[4] = (bBuffer[4] & 0x70);
bBuffer[4] = (bBuffer[4] | 0x80);
bBuffer[4] = (bBuffer[4] | GIF_code);
if (GIF_sort)
{
bBuffer[4] |= 0x08;
}
NSMutableData *GIF_string = [nsmutabledata datawithdata:[[nsstring stringwithstring:@"gif89a"] dataUsingEncoding: NSUTF8StringEncoding]];
[gif_screen setdata:[nsdata datawithbytes:bbuffer length:blength]];
[gif_string appenddata: gif_screen];
if (GIF_colorF == 1)
{
[self gifgetbytes:(3 * gif_size)];
[gif_string appenddata:gif_buffer];
}
else
{
[gif_string appenddata:gif_global];
}
// Add Graphic Control Extension Frame (for transparancy)
[gif_string appenddata:frame.header];
char endC = 0x2c;
[gif_string appendbytes:&endc length:sizeof(endc)];
size_t clength = [gif_screentmp length];
unsigned char cBuffer[clength];
[gif_screentmp getbytes:cbuffer length:clength];
cBuffer[8] &= 0x40;
[gif_screentmp setdata:[nsdata datawithbytes:cbuffer length:clength]];
[gif_string appenddata: gif_screentmp];
[self gifgetbytes:1];
[gif_string appenddata: gif_buffer];
while (true)
{
[self gifgetbytes:1];
[gif_string appenddata: gif_buffer];
unsigned char dBuffer[1];
[gif_buffer getbytes:dbuffer length:1];
long u = (long) dBuffer[0];
if (u != 0x00)
{
[self gifgetbytes:u];
[gif_string appenddata: gif_buffer];
}
else
{
break;
}
}
endC = 0x3b;
[gif_string appendbytes:&endc length:sizeof(endc)];
// save the frame into the array of frames
frame.data = GIF_string;
}
- (bool) GIFGetBytes:(int)length {
if (GIF_buffer != nil)
{
[gif_buffer release]; // Release old buffer
GIF_buffer = nil;
}
if ((NSInteger)[gif_pointer length] >= dataPointer + length) // Don't read across the edge of the file..
{
GIF_buffer = [[gif_pointer subdatawithrange:nsmakerange(datapointer, length)] retain];
dataPointer += length;
return YES;
}
else
{
return NO;
}
}
- (bool) GIFSkipBytes: (int) length {
if ((NSInteger)[gif_pointer length] >= dataPointer + length)
{
dataPointer += length;
return YES;
}
else
{
return NO;
}
}
- (NSData*) getFrameAsDataAtIndex:(int)index {
if (index < (NSInteger)[gif_frames count])
{
return ((AnimatedGifFrame *)[gif_frames objectatindex:index]).data;
}
else
{
return nil;
}
}
- (UIImage*) getFrameAsImageAtIndex:(int)index {
NSData *frameData = [self getframeasdataatindex: index];
UIImage *image = nil;
if (frameData != nil)
{
image = [uiimage imagewithdata:framedata];
}
return image;
}
@end