• 图片view设置gif动图


    这里有两张设置的方法
    第一种:引入UIImageView的子类SCGIFImageView(自定义的类)
    #import "SCGIFImageView.h"
    NSString* filePath  = [[NSBundle mainBundle] pathForResource:@"750-611" ofType:@"gif"];
    NSData* imageData   = [NSData dataWithContentsOfFile:filePath];
    [_dImg setData:imageData];
     
    //
    //  SCGIFImageView.h
    //  TestGIF
    //
    //  Created by shichangone on 11-7-12.
    //  Copyright 2011 __MyCompanyName__. All rights reserved.
    //
    /*
     Example:
     NSString* filePath  = [[NSBundle mainBundle] pathForResource:@"loading_gray" ofType:@"gif"];
     NSData* imageData   = [NSData dataWithContentsOfFile:filePath];
     [self.imageView setData:imageData];
     */
    
    #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;
    
    //Setting this value to pause or continue animation;
    @property (nonatomic) BOOL animating;
    
    - (void)setData:(NSData*)imageData;
    
    - (void)closeAnim;
    
    - (void)startAnim;
    
    - (void)stopAnim;
    
    @end
    SCGIFImageView.h
    //
    //  SCGIFImageView.m
    //  TestGIF
    //
    //  Created by shichangone on 11-7-12.
    //  Copyright 2011 __MyCompanyName__. All rights reserved.
    //
    
    #import "SCGIFImageView.h"
    #import <ImageIO/ImageIO.h>
    
    @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)closeAnim
    {
        [self release];
    }
    
    - (void)stopAnim
    {
        [self resetTimer];
        [_timer release];
    }
    
    - (void)startAnim
    {
        [self resetTimer];
        _currentImageIndex = -1;
        _animating = YES;
        [self showNextImage];
    }
    
    - (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];
        
        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 = 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 (!_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) {
            _animating = animating;
            if (!_timer) {
                [self showNextImage];
            }
        } else if (_animating && !animating) {
            _animating = animating;
            [self resetTimer];
        }
    }
    
    @end
    SCGIFImageView.m
    第二种:使用第三方框架sdwebimage
    #import "UIImage+GIF.h"
    NSString  *name = @"750-611";
        NSString  *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:name ofType:@"gif"];
        NSData  *imageData = [NSData dataWithContentsOfFile:filePath];
       
        _dImg.backgroundColor = [UIColor clearColor];
       
        _dImg.image = [UIImage sd_animatedGIFWithData:imageData];
       
  • 相关阅读:
    由类型名得到该类型
    TypeName of CA1505 is AvoidUnmaintainableCode, not AvoidUnmantainableCode.
    多核
    SSRS throws "The operation has timed out."
    XmlRootAttribute与XmlTypeAttribute
    littleendian and bigendian
    Assembly Exp
    如何在Ubuntu 11.10下成功安装Java [转载]
    *uck up ~,纯发泄
    [转载]读书相关,,
  • 原文地址:https://www.cnblogs.com/zh-li/p/5957228.html
Copyright © 2020-2023  润新知