iOS开发之SVProgressHUD加载Gif

2017-08-18  本文已影响0人  Ego_1973

我不会录gif图,录的mp4不能上传,所以只能发截图了😂

Simona_Test1 Simona_Test2

首先需要写一个UIImage+Gif的分类,我是直接更改了sd_webImage的代码,可以直接更改,也可以自己按照同样代码写一个.

 #import "SDWebImageCompat.h"

 @interface UIImage (GIF)

 - (BOOL)isGIF;

 + (UIImage *)sd_animatedGIFNamed:(NSString *)name;

 @end

@implementation UIImage (GIF)

 + (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];
}
}

- (BOOL)isGIF {
return (self.images != nil);
}

+ (UIImage *)sd_animatedGIFWithData:(NSData *)data {
if (!data) {
    return nil;
}

CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);

size_t count = CGImageSourceGetCount(source);

UIImage *animatedImage;

if (count <= 1) {
    animatedImage = [[UIImage alloc] initWithData:data];
}
else {
    NSMutableArray *images = [NSMutableArray array];
    
    NSTimeInterval duration = 0.0f;
    
    for (size_t i = 0; 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 = (1.0f / 10.0f) * count;
    }
    
    animatedImage = [UIImage animatedImageWithImages:images duration:duration];
}

CFRelease(source);

return animatedImage;
}

+ (float)sd_frameDurationAtIndex:(NSUInteger)index source:(CGImageSourceRef)source {
float frameDuration = 0.1f;
CFDictionaryRef cfFrameProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil);
NSDictionary *frameProperties = (__bridge NSDictionary *)cfFrameProperties;
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 < 0.011f) {
    frameDuration = 0.100f;
}

CFRelease(cfFrameProperties);
return frameDuration;
}

SVProgressHUD.m
这里的SVProgressView我亦是更改了源代码,因为我所有的显示都是用同一个Gif加载的.此处修改了SVProgressHUD.m- (void)showImage:(UIImage*)image status:(NSString*)status duration:(NSTimeInterval)duration的方法

- (void)showImage:(UIImage*)image status:(NSString*)status duration:(NSTimeInterval)duration {
__weak SVProgressHUD *weakSelf = self;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
    __strong SVProgressHUD *strongSelf = weakSelf;
    if(strongSelf){
        // Update / Check view hierarchy to ensure the HUD is visible
        [strongSelf updateViewHierarchy];
        
        // Reset progress and cancel any running animation
        strongSelf.progress = SVProgressHUDUndefinedProgress;
        [strongSelf cancelRingLayerAnimation];
        [strongSelf cancelIndefiniteAnimatedViewAnimation];
        
        // Update imageView
        UIColor *tintColor = strongSelf.foregroundColorForStyle;
        UIImage *tintedImage = image;
        if (image.renderingMode != UIImageRenderingModeAlwaysTemplate) {
            tintedImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        }
        strongSelf.imageView.tintColor = tintColor;
        strongSelf.imageView.image = tintedImage;
        strongSelf.imageView.hidden = NO;
        
        ///////////此处为修改代码///////
        // 设置gif视图的大小
        CGRect imageFrame = strongSelf.imageView.frame;
        imageFrame.size = CGSizeMake(60, 60);
        strongSelf.imageView.frame = imageFrame;
        
        
        // Update text
        strongSelf.statusLabel.text = status;
        
        // Show
        [strongSelf showStatus:status];
        
        // 此处屏蔽是因为这个的加载时间是根据字符串的长度,关闭之后消失直接调用[SVProgressHUD dismiss];
        // An image will dismissed automatically. Therefore we start a timer
        // which then will call dismiss after the predefined duration
       // strongSelf.fadeOutTimer = [NSTimer timerWithTimeInterval:duration target:strongSelf selector:@selector(dismiss) userInfo:nil repeats:NO];
       // [[NSRunLoop mainRunLoop] addTimer:strongSelf.fadeOutTimer forMode:NSRunLoopCommonModes];
    }
}];
}

ViewController调用:

// 显示
[SVProgressHUD setInfoImage:[UIImage sd_animatedGIFNamed:@"source"]];
[SVProgressHUD showInfoWithStatus:@"加载中..."];
// 消失
// [SVProgressHUD dismiss];
上一篇下一篇

猜你喜欢

热点阅读