UIImageView支持动图(gif)

2016-10-07  本文已影响48人  xxzsxxzs

思路一 给UIImageView 扩展一个Category
基于Image I/O CGImageSourceRef、CGImageDestinationRef

CFRelease(source);
}

char graphicControlExtensionStartBytes[] = {0x21,0xF9,0x04};

double duration=0;

NSRange dataSearchLeftRange = NSMakeRange(0, data.length);

while(YES){

NSRange frameDescriptorRange = [data rangeOfData:[NSData dataWithBytes:graphicControlExtensionStartBytes

length:3]

options:NSDataSearchBackwards

range:dataSearchLeftRange];

if(frameDescriptorRange.location!=NSNotFound){

NSData *durationData = [data subdataWithRange:NSMakeRange(frameDescriptorRange.location+4, 2)];

unsigned char buffer[2];

[durationData getBytes:buffer];

double delay = (buffer[0] | buffer[1] << 8);

duration += delay;

dataSearchLeftRange = NSMakeRange(0, frameDescriptorRange.location);

}else{

break;

}

}

return duration/100;

}

andDuration:(NSTimeInterval) duration {

if (!source) return;

size_t count = CGImageSourceGetCount(source);

NSMutableArray *images = [NSMutableArray arrayWithCapacity:count];

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

CGImageRef cgImage = CGImageSourceCreateImageAtIndex(source, i, NULL);

if (!cgImage)

return;

[images addObject:[UIImage imageWithCGImage:cgImage]];

CGImageRelease(cgImage);

}

[self setAnimationImages:images];

[self setAnimationDuration:duration];

[self startAnimating];
}

思路二:已知webview 是可以加载gif图的
注意:关闭webview的交互。。。

上一篇下一篇

猜你喜欢

热点阅读