码迷,mamicode.com
首页 > 其他好文 > 详细

图片view设置gif动图

时间:2016-10-13 19:20:33      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:

这里有两张设置的方法
第一种:引入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];
   

图片view设置gif动图

标签:

原文地址:http://www.cnblogs.com/zh-li/p/5957228.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!