zoukankan      html  css  js  c++  java
  • iOS gif图显示问题

    问题

    有时候需要显示gif动态图,让界面更加的绚丽,但是iOS默认只支持png,gpg图片。那么如何才能显示gif图呢?

    解决方式

    1. 添加框架

      CoreGraphics.framework

      ImageIO.framework

    2. 引入SCGIFImageView类

    SCGIFImageView.h

        #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
    
    1. 添加gif图片

      在Images.xcassets中添加需要的gif动态图。

    2. 使用方式

      • 引入头文件SCGIFImageView.h
      • 在需要的添加的地方添加以下代码

           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];
        
  • 相关阅读:
    According to TLD or attribute directive in tag file, attribute end does not accept any expressions
    Several ports (8080, 8009) required by Tomcat v6.0 Server at localhost are already in use.
    sql注入漏洞
    Servlet—简单的管理系统
    ServletContext与网站计数器
    VS2010+ICE3.5运行官方demo报错----std::bad_alloc
    java 使用相对路径读取文件
    shell编程 if 注意事项
    Ubuntu12.04下eclipse提示框黑色背景色的修改方法
    解决Ubuntu环境变量错误导致无法正常登录
  • 原文地址:https://www.cnblogs.com/AliliWl/p/4895285.html
Copyright © 2011-2022 走看看