// // SCGIFImageView.m // TestGIF // // Created by shichangone on 11-7-12. // Copyright 2011 __MyCompanyName__. All rights reserved. // #import "SCGIFImageView.h" #import @implementation SCGIFImageFrame @synthesize image = _image; @synthesize duration = _duration; - (void)dealloc { [_image release]; [super dealloc]; } @end @interface SCGIFImageView () - (void)resetTimer; - (void)showNextImage; @end @implementation SCGIFImageView @synthesize imageFrameArray = _imageFrameArray; @synthesize timer = _timer; @synthesize animating = _animating; - (void)dealloc { [self resetTimer]; [_imageFrameArray release]; [_timer release]; [super dealloc]; } - (void)resetTimer { if (_timer && _timer.isValid) { [_timer invalidate]; } self.timer = nil; } - (void)setData:(NSData *)imageData { if (!imageData) { return; } [self resetTimer]; CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef)imageData, NULL); size_t count = CGImageSourceGetCount(source); NSMutableArray* tmpArray = [NSMutableArray array]; _isEndPlay = NO; for (size_t i = 0; i < count; i++) { SCGIFImageFrame* gifImage = [[[SCGIFImageFrame alloc] init] autorelease]; CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL); gifImage.image = [UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]; NSDictionary* frameProperties = [(NSDictionary*)CGImageSourceCopyPropertiesAtIndex(source, i, NULL) autorelease]; gifImage.duration = [[[frameProperties objectForKey:(NSString*)kCGImagePropertyGIFDictionary] objectForKey:(NSString*)kCGImagePropertyGIFDelayTime] doubleValue]; // gifImage.duration = 0.05; [tmpArray addObject:gifImage]; CGImageRelease(image); } CFRelease(source); self.imageFrameArray = nil; if (tmpArray.count > 1) { self.imageFrameArray = tmpArray; _currentImageIndex = -1; _animating = YES; [self showNextImage]; } else { self.image = [UIImage imageWithData:imageData]; } } - (void)setImage:(UIImage *)image { [super setImage:image]; [self resetTimer]; self.imageFrameArray = nil; _animating = NO; } - (void)showNextImage { if (!_animating) { return; } _currentImageIndex = (++_currentImageIndex) % _imageFrameArray.count; SCGIFImageFrame* gifImage = [_imageFrameArray objectAtIndex:_currentImageIndex]; [super setImage:[gifImage image]]; // if (_currentImageIndex == self.imageFrameArray.count - 1) { // _isEndPlay = YES; // } if (_isEndPlay && _currentImageIndex == 0) { return; } self.timer = [NSTimer scheduledTimerWithTimeInterval:gifImage.duration target:self selector:@selector(showNextImage) userInfo:nil repeats:NO]; [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes]; } - (void)setAnimating:(BOOL)animating { if (_imageFrameArray.count < 2) { _animating = animating; return; } if (animating) { //continue _animating = animating; _isEndPlay = NO; [self resetTimer]; if (!_timer) { [self showNextImage]; } } else if (!animating) { //stop _animating = animating; [self resetTimer]; } } @end