天天看點

iOS學習筆記-126.SDWebImage2——顯示gif圖檔126.SDWebImage2——顯示gif圖檔

[TOC

126.SDWebImage2——顯示gif圖檔

一、說明

其實我們的 GIF分類就是把我們的 GIF差分為一張一張的圖檔 ,然後再用 UIImageView 的view動畫,來播放這些圖檔。

二、UIImage+GIF 分類

2.1 UIImage+GIF.h

//
//  UIImage+GIF.h
//  LBGIFImage
//
//  Created by Laurin Brandner on 06.01.12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UIImage (GIF)

//傳入Gif圖像的名稱,得到一個可動畫的圖像
+ (UIImage *)sd_animatedGIFNamed:(NSString *)name;

//傳入Gif圖像的二進制資料,得到一個可動畫的圖像
+ (UIImage *)sd_animatedGIFWithData:(NSData *)data;

- (UIImage *)sd_animatedImageByScalingAndCroppingToSize:(CGSize)size;

@end
           

2.2 UIImage+GIF.m

//
//  UIImage+GIF.m
//  LBGIFImage
//
//  Created by Laurin Brandner on 06.01.12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "UIImage+GIF.h"
#import <ImageIO/ImageIO.h>

@implementation UIImage (GIF)

//把圖檔的二進制資料轉換為圖檔(GIF)
+ (UIImage *)sd_animatedGIFWithData:(NSData *)data {

    //如果傳入的二進制資料為空,則直接傳回nil
    if (!data) {
        return nil;
    }

     // 建立圖像源
    CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);

    // 擷取圖檔幀數
    size_t count = CGImageSourceGetCount(source);

    //初始化animatedImage
    UIImage *animatedImage;

    //如果圖檔幀數小于等于1,那麼就直接把二進制資料轉換為圖檔,并傳回圖檔
    if (count <= ) {
        animatedImage = [[UIImage alloc] initWithData:data];
    }
    else {
        //建立可變的空的圖檔數組
        NSMutableArray *images = [NSMutableArray array];

        //初始化動畫播放時間為0
        NSTimeInterval duration = f;

        // 周遊并且提取所有的動畫幀
        for (size_t i = ; i < count; i++) {
            CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);

            // 累加動畫時長
            duration += [self sd_frameDurationAtIndex:i source:source];

            // 将圖像添加到動畫數組
            [images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]];

            //釋放操作
            CGImageRelease(image);
        }

        //計算動畫時間
        if (!duration) {
            duration = (f / f) * count;
        }
        // 建立可動畫圖像
        animatedImage = [UIImage animatedImageWithImages:images duration:duration];
    }

    //釋放操作
    CFRelease(source);

    return animatedImage;
}

//獲得播放的時間長度
+ (float)sd_frameDurationAtIndex:(NSUInteger)index source:(CGImageSourceRef)source {
    float frameDuration = f;
    //獲得圖像的屬性(圖像源,索引)
    CFDictionaryRef cfFrameProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil);
    //橋接轉換為NSDictionary類型
    NSDictionary *frameProperties = (__bridge NSDictionary *)cfFrameProperties;
    //取出圖像屬性裡面kCGImagePropertyGIFDictionary這個KEY對應的值,即GIF屬性
    NSDictionary *gifProperties = frameProperties[(NSString *)kCGImagePropertyGIFDictionary];

    //得到延遲時間
    NSNumber *delayTimeUnclampedProp = gifProperties[(NSString *)kCGImagePropertyGIFUnclampedDelayTime];
    if (delayTimeUnclampedProp) {
        //把延遲時間轉換為浮點數類型
        frameDuration = [delayTimeUnclampedProp floatValue];
    }
    else {
        //如果上面獲得的延遲時間為空,則換另外一種方式獲得
        NSNumber *delayTimeProp = gifProperties[(NSString *)kCGImagePropertyGIFDelayTime];
        if (delayTimeProp) {
            frameDuration = [delayTimeProp floatValue];
        }
    }

    // Many annoying ads specify a 0 duration to make an image flash as quickly as possible.
    // We follow Firefox's behavior and use a duration of 100 ms for any frames that specify
    // a duration of <= 10 ms. See <rdar://problem/7689300> and <http://webkit.org/b/36082>
    // for more information.

    //處理延遲時間
    if (frameDuration < f) {
        frameDuration = f;
    }

    //釋放操作
    CFRelease(cfFrameProperties);
    return frameDuration;
}

//處理GIF圖檔
+ (UIImage *)sd_animatedGIFNamed:(NSString *)name {
    //獲得scale
    CGFloat scale = [UIScreen mainScreen].scale;

    if (scale > f) {
        //根據圖檔的名稱拼接bundle全路徑
        NSString *retinaPath = [[NSBundle mainBundle] pathForResource:[name stringByAppendingString:@"@2x"] ofType:@"gif"];

        //加載指定路徑的圖檔(二進制資料)
        NSData *data = [NSData dataWithContentsOfFile:retinaPath];

        //如果data不為空,則直接調用sd_animatedGIFWithData傳回一張可動畫的圖檔
        if (data) {
            return [UIImage sd_animatedGIFWithData:data];
        }
        //下面的處理和上面一樣
        NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"];

        data = [NSData dataWithContentsOfFile:path];

        if (data) {
            return [UIImage sd_animatedGIFWithData:data];
        }

        return [UIImage imageNamed:name];
    }
    else {
        NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"];

        NSData *data = [NSData dataWithContentsOfFile:path];

        if (data) {
            return [UIImage sd_animatedGIFWithData:data];
        }

        return [UIImage imageNamed:name];
    }
}

//縮放|裁剪...
- (UIImage *)sd_animatedImageByScalingAndCroppingToSize:(CGSize)size {
    //如果尺寸相等或者是為0則直接傳回
    if (CGSizeEqualToSize(self.size, size) || CGSizeEqualToSize(size, CGSizeZero)) {
        return self;
    }

    CGSize scaledSize = size;
    CGPoint thumbnailPoint = CGPointZero;

    CGFloat widthFactor = size.width / self.size.width;
    CGFloat heightFactor = size.height / self.size.height;
    CGFloat scaleFactor = (widthFactor > heightFactor) ? widthFactor : heightFactor;
    scaledSize.width = self.size.width * scaleFactor;
    scaledSize.height = self.size.height * scaleFactor;

    if (widthFactor > heightFactor) {
        thumbnailPoint.y = (size.height - scaledSize.height) * ;
    }
    else if (widthFactor < heightFactor) {
        thumbnailPoint.x = (size.width - scaledSize.width) * ;
    }

    //初始化可變的縮放圖像數組
    NSMutableArray *scaledImages = [NSMutableArray array];

    //周遊圖檔
    for (UIImage *image in self.images) {
        //開啟圖像上下文
        UIGraphicsBeginImageContextWithOptions(size, NO, );
        //畫圖,把image繪制到指定的位置
        [image drawInRect:CGRectMake(thumbnailPoint.x, thumbnailPoint.y, scaledSize.width, scaledSize.height)];
        //根據目前圖形上下文獲得一張新的圖檔
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        //把新圖檔添加到圖像數組
        [scaledImages addObject:newImage];

        //關閉圖形上下文
        UIGraphicsEndImageContext();
    }

    //建立可動畫的圖像并傳回
    return [UIImage animatedImageWithImages:scaledImages duration:self.duration];
}

@end
           

三、示例代碼

/*gif圖檔顯示*/
-(void)gif{
    UIImage *image = [UIImage sd_animatedGIFNamed:@"20140310135421_BJ4UP"];
    self.imageView.image =image;
}
           

四、圖示

iOS學習筆記-126.SDWebImage2——顯示gif圖檔126.SDWebImage2——顯示gif圖檔