iOS開(kāi)發(fā)之SVProgressHUD加載Gif

我不會(huì)錄gif圖,錄的mp4不能上傳,所以只能發(fā)截圖了??

Simona_Test1
Simona_Test2

首先需要寫(xiě)一個(gè)UIImage+Gif的分類,我是直接更改了sd_webImage的代碼,可以直接更改,也可以自己按照同樣代碼寫(xiě)一個(gè).

 #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我亦是更改了源代碼,因?yàn)槲宜械娘@示都是用同一個(gè)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;
        
        ///////////此處為修改代碼///////
        // 設(shè)置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];
        
        // 此處屏蔽是因?yàn)檫@個(gè)的加載時(shí)間是根據(jù)字符串的長(zhǎng)度,關(guān)閉之后消失直接調(diào)用[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調(diào)用:

// 顯示
[SVProgressHUD setInfoImage:[UIImage sd_animatedGIFNamed:@"source"]];
[SVProgressHUD showInfoWithStatus:@"加載中..."];
// 消失
// [SVProgressHUD dismiss];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末爬泥,一起剝皮案震驚了整個(gè)濱河市材泄,隨后出現(xiàn)的幾起案子梅掠,更是在濱河造成了極大的恐慌磷蛹,老刑警劉巖冤留,帶你破解...
    沈念sama閱讀 218,755評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件衷快,死亡現(xiàn)場(chǎng)離奇詭異拷肌,居然都是意外死亡馋袜,警方通過(guò)查閱死者的電腦和手機(jī)黔攒,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門趁啸,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人督惰,你說(shuō)我怎么就攤上這事不傅。” “怎么了赏胚?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,138評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵访娶,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我觉阅,道長(zhǎng)崖疤,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,791評(píng)論 1 295
  • 正文 為了忘掉前任典勇,我火速辦了婚禮劫哼,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘割笙。我一直安慰自己权烧,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,794評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布伤溉。 她就那樣靜靜地躺著豪嚎,像睡著了一般。 火紅的嫁衣襯著肌膚如雪谈火。 梳的紋絲不亂的頭發(fā)上侈询,一...
    開(kāi)封第一講書(shū)人閱讀 51,631評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音糯耍,去河邊找鬼扔字。 笑死,一個(gè)胖子當(dāng)著我的面吹牛温技,可吹牛的內(nèi)容都是我干的革为。 我是一名探鬼主播,決...
    沈念sama閱讀 40,362評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼舵鳞,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼震檩!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,264評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤抛虏,失蹤者是張志新(化名)和其女友劉穎博其,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體迂猴,經(jīng)...
    沈念sama閱讀 45,724評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡慕淡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了沸毁。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片峰髓。...
    茶點(diǎn)故事閱讀 40,040評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖息尺,靈堂內(nèi)的尸體忽然破棺而出携兵,到底是詐尸還是另有隱情,我是刑警寧澤搂誉,帶...
    沈念sama閱讀 35,742評(píng)論 5 346
  • 正文 年R本政府宣布徐紧,位于F島的核電站,受9級(jí)特大地震影響勒葱,放射性物質(zhì)發(fā)生泄漏浪汪。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,364評(píng)論 3 330
  • 文/蒙蒙 一凛虽、第九天 我趴在偏房一處隱蔽的房頂上張望死遭。 院中可真熱鬧,春花似錦凯旋、人聲如沸呀潭。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,944評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)钠署。三九已至,卻和暖如春荒椭,著一層夾襖步出監(jiān)牢的瞬間谐鼎,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,060評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工趣惠, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留狸棍,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,247評(píng)論 3 371
  • 正文 我出身青樓味悄,卻偏偏與公主長(zhǎng)得像草戈,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子侍瑟,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,979評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容