iOS展示gif圖

展示本地gif圖片
SDWebImage比較占內(nèi)存,FLAnimatedImage不怎么占用內(nèi)存

1.使用SDWebImage

  1. 導(dǎo)入頭文件
    #import "UIImage+GIF.h"
  2. 設(shè)置gif圖
    self.imageView.image = [UIImage sd_animatedGIFNamed:@"test"];
    也可以通過(guò)NSData導(dǎo)入
    + (UIImage *)sd_animatedGIFWithData:(NSData *)data

2.使用FLAnimatedImage

  1. 導(dǎo)入頭文件
    #import "FLAnimatedImage.h"
  2. 設(shè)置gif圖
    NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"gif"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:data];
    self.imageView.animatedImage = image;

3.SDWebImage實(shí)現(xiàn)原理

使用UIImage + GIF分類

1.將gif圖的每一幀導(dǎo)出為一個(gè)UIImage,并且算出每一幀的播放時(shí)間,將導(dǎo)出的image放入images數(shù)組中
2.使用+ animatedImageWithImages: duration: 方法創(chuàng)建image,參數(shù)為第一步的images數(shù)組和播放總時(shí)長(zhǎng)
3.設(shè)置UIImageView的image時(shí)用sd_animatedGIFNamed:方法即可展示gif

方法1: 通過(guò)NSData獲取gif的UIImage
方法名:+ (UIImage )sd_animatedGIFWithData:(NSData *)data;
  1. 獲取gif數(shù)據(jù),及其幀數(shù)
  2. 獲取每幀的像素位圖,求出每幀的播放時(shí)間
  3. 按照屏幕分辨率生成UIImage并放入數(shù)組
  4. 通過(guò)+ animatedImageWithImages: duration:生成UIImage
    + (UIImage *)sd_animatedGIFWithData:(NSData *)data {
    if (!data) {
    return nil;
    }
    //通過(guò)CFData讀取gif文件的數(shù)據(jù)
    CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
    //獲取gif文件的幀數(shù)
    size_t count = CGImageSourceGetCount(source);
    UIImage *animatedImage;
    if (count <= 1) {
    animatedImage = [[UIImage alloc] initWithData:data];
    }
    else {//大于一張圖片時(shí)
    NSMutableArray *images = [NSMutableArray array];
    //設(shè)置gif播放的時(shí)間
    NSTimeInterval duration = 0.0f;
    for (size_t i = 0; i < count; i++) {
    //獲取gif指定幀的像素位圖
    CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);
    if (!image) {
    continue;
    }
    //獲取每張圖的播放時(shí)間
    duration += [self sd_frameDurationAtIndex:i source:source];
    [images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]];
    CGImageRelease(image);
    }
    if (!duration) {//如果播放時(shí)間為空
    duration = (1.0f / 10.0f) * count;
    }
    animatedImage = [UIImage animatedImageWithImages:images duration:duration];
    }
    CFRelease(source);
    return animatedImage;
    }
方法2:獲取指定幀的image的播放時(shí)間
方法名:+ (float)sd_frameDurationAtIndex:(NSUInteger)index source:(CGImageSourceRef)source
  1. 獲取指定幀圖片的屬性字典
  2. 獲取圖片的gif屬性字典
  3. 獲取每一幀的播放時(shí)間(如果小于0.1,設(shè)置為0.1)
    + (float)sd_frameDurationAtIndex:(NSUInteger)index source:(CGImageSourceRef)source {
    float frameDuration = 0.1f;
    //獲取這一幀圖片的屬性字典
    CFDictionaryRef cfFrameProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil);
    NSDictionary *frameProperties = (__bridge NSDictionary *)cfFrameProperties;
    //獲取gif屬性字典
    NSDictionary *gifProperties = frameProperties[(NSString *)kCGImagePropertyGIFDictionary];
    //獲取這一幀持續(xù)的時(shí)間
    NSNumber *delayTimeUnclampedProp = gifProperties[(NSString *)kCGImagePropertyGIFUnclampedDelayTime];
    if (delayTimeUnclampedProp) {
    frameDuration = [delayTimeUnclampedProp floatValue];
    }
    else {
    NSNumber *delayTimeProp = gifProperties[(NSString *)kCGImagePropertyGIFDelayTime];
    if (delayTimeProp) {
    frameDuration = [delayTimeProp floatValue];
    }
    }
    //如果幀數(shù)小于0.1,則指定為0.1
    if (frameDuration < 0.011f) {
    frameDuration = 0.100f;
    }
    CFRelease(cfFrameProperties);
    return frameDuration;
    }
方法3:通過(guò)圖片的Name獲取UIImage
方法名:+ (UIImage *)sd_animatedGIFNamed:(NSString *)name;
  1. 根據(jù)屏幕分辨率給圖片自動(dòng)加后綴(@2x)
  2. 從mainBundle中獲取Data數(shù)據(jù)
  3. 調(diào)用方法 + sd_animatedGIFWithData:
    + (UIImage *)sd_animatedGIFNamed:(NSString *)name {
    //判斷屏幕分辨率
    CGFloat scale = [UIScreen mainScreen].scale;
    if (scale > 1.0f) {
    NSString *retinaPath = [[NSBundle mainBundle] pathForResource:[name stringByAppendingString:@"@2x"] ofType:@"gif"];
    NSData *data = [NSData dataWithContentsOfFile:retinaPath];
    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];
    }
    }
方法4:對(duì)gif按比例進(jìn)行縮放
方法名:- (UIImage *)sd_animatedImageByScalingAndCroppingToSize:(CGSize)size;
  1. 獲取較大的縮放比例值,寬高等比縮放
  2. 調(diào)整位置,使縮放后的圖居中
  3. 遍歷self.images, 將圖片縮放后導(dǎo)出放入數(shù)組
  4. 通過(guò)animatedImageWithImages:duration:返回image
    - (UIImage *)sd_animatedImageByScalingAndCroppingToSize:(CGSize)size {
    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;
    //調(diào)整位置,使縮放后的圖居中
    if (widthFactor > heightFactor) {
    thumbnailPoint.y = (size.height - scaledSize.height) * 0.5;
    }
    else if (widthFactor < heightFactor) {
    thumbnailPoint.x = (size.width - scaledSize.width) * 0.5;
    }
    //遍歷self.images, 將圖片縮放后導(dǎo)出放入數(shù)組
    NSMutableArray *scaledImages = [NSMutableArray array];
    for (UIImage *image in self.images) {
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    [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];
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容