YYImage之YYImage

YYImage 是框架對圖片的封裝對象,它支持 GIF, APNG,WebP 格式的動(dòng)畫圖片。

初始化圖片

生成一張 YYImage 會(huì)調(diào)用

- (instancetype)initWithData:(NSData *)data scale:(CGFloat)scale;

來初始化,具體代碼如下

- (instancetype)initWithData:(NSData *)data scale:(CGFloat)scale {
    if (data.length == 0) return nil;
    if (scale <= 0) scale = [UIScreen mainScreen].scale;
    _preloadedLock = dispatch_semaphore_create(1);
    @autoreleasepool {
        YYImageDecoder *decoder = [YYImageDecoder decoderWithData:data scale:scale];
        YYImageFrame *frame = [decoder frameAtIndex:0 decodeForDisplay:YES];
        UIImage *image = frame.image;
        if (!image) return nil;
        self = [self initWithCGImage:image.CGImage scale:decoder.scale orientation:image.imageOrientation];
        if (!self) return nil;
        _animatedImageType = decoder.type;
        if (decoder.frameCount > 1) {
            _decoder = decoder;
            _bytesPerFrame = CGImageGetBytesPerRow(image.CGImage) * CGImageGetHeight(image.CGImage);
            _animatedImageMemorySize = _bytesPerFrame * decoder.frameCount;
        }
        self.yy_isDecodedForDisplay = YES;
    }
    return self;
}

YYImage 內(nèi)部維護(hù)了一個(gè)YYImageDecoder對象來進(jìn)行圖片的編碼和解碼,YYImageDecoder是一個(gè)強(qiáng)大的對象,它可以提供圖片的解碼,和編碼,并且在 iOS6 級別提供 apng 和 webp 的解碼,YYImageDecoder不是本文介紹的重點(diǎn),將會(huì)在以后的文章中重點(diǎn)介紹,所以可以先忽略,知道它是一個(gè)圖片的解碼者就行啦。

展示在 YYAnimatedImageView 上

老樣子,想在 YYAnimatedImageView 上顯示需要遵守協(xié)議 YYAnimatedImage
來看看如何實(shí)現(xiàn)協(xié)議

告訴 YYAnimatedImageView 幀數(shù),播放次數(shù),每一幀的字節(jié)大小,

- (NSUInteger)animatedImageFrameCount {
    return _decoder.frameCount;
}

- (NSUInteger)animatedImageLoopCount {
    return _decoder.loopCount;
}

- (NSUInteger)animatedImageBytesPerFrame {
    return _bytesPerFrame;
}

返回每一幀的圖片和每一幀的顯示時(shí)間

- (UIImage *)animatedImageFrameAtIndex:(NSUInteger)index {
    if (index >= _decoder.frameCount) return nil;
    dispatch_semaphore_wait(_preloadedLock, DISPATCH_TIME_FOREVER);
    UIImage *image = _preloadedFrames[index];
    dispatch_semaphore_signal(_preloadedLock);
    if (image) return image == (id)[NSNull null] ? nil : image;
    return [_decoder frameAtIndex:index decodeForDisplay:YES].image;
}

- (NSTimeInterval)animatedImageDurationAtIndex:(NSUInteger)index {
    NSTimeInterval duration = [_decoder frameDurationAtIndex:index];
    
    /*
     http://opensource.apple.com/source/WebCore/WebCore-7600.1.25/platform/graphics/cg/ImageSourceCG.cpp
     Many annoying ads specify a 0 duration to make an image flash as quickly as 
     possible. We follow Safari and 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.
     
     See also: http://nullsleep.tumblr.com/post/16524517190/animated-gif-minimum-frame-delay-browser.
     */
    if (duration < 0.011f) return 0.100f;
    return duration;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

  • 圖片通常是移動(dòng)端流量耗費(fèi)最多的部分,并且占據(jù)著重要的視覺空間。合理的圖片格式選用和優(yōu)化可以為你節(jié)省帶寬、提升視覺效...
    傻傻小蘿卜閱讀 873評論 1 9
  • 移動(dòng)端圖片格式調(diào)研 圖片通常是移動(dòng)端流量耗費(fèi)最多的部分,并且占據(jù)著重要的視覺空間。合理的圖片格式選用和優(yōu)化可以為你...
    AngeloD閱讀 1,319評論 0 5
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,271評論 4 61
  • runtime是什么? oc對象 看看oc對象的定義 看看Class oc有幾種對象 舉例:現(xiàn)在我有兩個(gè)類,Cla...
    我是C閱讀 221評論 0 3
  • 1.不高興小姐的不高興生活 之所以叫不高興小姐,顧名思義,把情緒都寫在臉上,不管正面還是負(fù)面情緒,永遠(yuǎn)一副很酷的樣...
    一只羊咩咩_閱讀 3,403評論 16 9

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