問題web
有時候須要顯示gif動態圖,讓界面更加的絢麗,可是iOS默認只支持png,gpg圖片。那麼如何才能顯示gif圖呢?框架
添加框架ide
CoreGraphics.frameworkthis
ImageIO.frameworkatom
引入SCGIFImageView類spa
SCGIFImageView.hcode
#import <UIKit/UIKit.h> @interface SCGIFImageFrame : NSObject { } @property (nonatomic) double duration; @property (nonatomic, retain) UIImage* image; @end @interface SCGIFImageView : UIImageView { NSInteger _currentImageIndex; } @property (nonatomic, retain) NSArray* imageFrameArray; @property (nonatomic, retain) NSTimer* timer; @property (nonatomic, assign) NSInteger sumCount; //Setting this value to pause or continue animation; @property (nonatomic) BOOL animating; - (void)setData:(NSData*)imageData; @end
SCGIFImageView.m圖片
#import "SCGIFImageView.h" #import <ImageIO/ImageIO.h> @implementation SCGIFImageFrame @synthesize image = _image; @synthesize duration = _duration; @end @interface SCGIFImageView () - (void)resetTimer; - (void)showNextImage; @end @implementation SCGIFImageView @synthesize imageFrameArray = _imageFrameArray; @synthesize timer = _timer; @synthesize animating = _animating; - (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]; for (size_t i = 0; i < count; i++) { SCGIFImageFrame* gifImage = [[SCGIFImageFrame alloc] init]; CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL); gifImage.image = [UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]; NSDictionary* frameProperties = CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source, i, NULL)); gifImage.duration = [[[frameProperties objectForKey:(NSString*)kCGImagePropertyGIFDictionary] objectForKey:(NSString*)kCGImagePropertyGIFDelayTime] doubleValue]; gifImage.duration = MAX(gifImage.duration, 0.01); [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 (_sumCount > 0) { _animating = _sumCount - 1 == _currentImageIndex ? NO : YES; } if (!_animating) { return; } _currentImageIndex = (++_currentImageIndex) % _imageFrameArray.count; SCGIFImageFrame* gifImage = [_imageFrameArray objectAtIndex:_currentImageIndex]; [super setImage:[gifImage image]]; self.timer = [NSTimer scheduledTimerWithTimeInterval:gifImage.duration target:self selector:@selector(showNextImage) userInfo:nil repeats:NO]; } - (void)setAnimating:(BOOL)animating { if (_imageFrameArray.count < 2) { _animating = animating; return; } if (!_animating && animating) { //continue _animating = animating; if (!_timer) { [self showNextImage]; } } else if (_animating && !animating) { //stop _animating = animating; [self resetTimer]; } } @end
添加gif圖片get
在Images.xcassets中添加須要的gif動態圖。animation
使用方式
在須要的添加的地方添加如下代碼
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"6plus_hover.gif" ofType:nil]; NSData* imageData = [NSData dataWithContentsOfFile:filePath]; SCGIFImageView* gifImageView = [[SCGIFImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)]; [gifImageView setData:imageData]; [gifImageView setSumCount:gifImageView.imageFrameArray.count];//設置只循環一次,不設置無限循環 [self.view addSubview:gifImageView];