YYImage源碼解析<一> 簡(jiǎn)單使用

YYImage學(xué)習(xí)筆記<一> 簡(jiǎn)單使用

前言

YYImage開源庫包含5個(gè)類文件,分別為:YYImage、YYFrameImage、YYSpriteSheetImage、YYImageCoder、YYAnimaeImageView。

功能簡(jiǎn)介如下:
YYImage:加載單張圖片,包括gif,WebP,APNG等圖片(YYImageDemo里面主要測(cè)試?yán)镞@三種)
YYFrameImage:加載多張圖使用
YYSpriteSheetImage:加載精靈圖片,就是多個(gè)圖片在一張圖紙上,通過計(jì)算坐標(biāo)來取圖
YYImageCoder:用于對(duì)圖片文件進(jìn)行編解碼
YYAnimaeImageView:實(shí)現(xiàn)動(dòng)畫效果

簡(jiǎn)單使用

1、加載gif圖(使用YYImage類)

- (void)viewDidLoad {
    // 加載圖片
    YYImage * image = [YYImage imageNamed:@"niconiconi"];
    // 類似系統(tǒng)的UIImageView(這里作者重寫用于顯示gif、WebP和APNG格式圖片)
    YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(0, 70, 300, 300);
    [self.view addSubview:imageView];
}

2 加載WebP格式圖片(使用YYImage類)

- (void)viewDidLoad {
    // 加載圖片
    YYImage * image = [YYImage imageNamed:@"wall-e"];
    // 類似系統(tǒng)的UIImageView(這里作者重寫用于顯示gif、WebP和APNG格式圖片)
    YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(0, 70, 300, 300);
    [self.view addSubview:imageView];
}

3 加載APNG格式圖片(使用YYImage類)

- (void)viewDidLoad {
    // 加載圖片
    YYImage * image = [YYImage imageNamed:@"pia"];
    // 類似系統(tǒng)的UIImageView(這里作者重寫用于顯示gif、WebP和APNG格式圖片)
    YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(0, 70, 300, 300);
    [self.view addSubview:imageView];
}

4 加載多圖(使用YYFrameImage類)

- (void)viewDidLoad {
    [super viewDidLoad];
  
    // 獲取圖片路徑
    NSString *basePath = [[NSBundle mainBundle].bundlePath stringByAppendingPathComponent:@"EmoticonWeibo.bundle/com.sina.default"];
    
    // 拼接圖片路徑并添加數(shù)組
    NSMutableArray *paths = [NSMutableArray new];
    [paths addObject:[basePath stringByAppendingPathComponent:@"d_aini@3x.png"]];
    [paths addObject:[basePath stringByAppendingPathComponent:@"d_baibai@3x.png"]];
    [paths addObject:[basePath stringByAppendingPathComponent:@"d_chanzui@3x.png"]];
    [paths addObject:[basePath stringByAppendingPathComponent:@"d_chijing@3x.png"]];
    [paths addObject:[basePath stringByAppendingPathComponent:@"d_dahaqi@3x.png"]];
    [paths addObject:[basePath stringByAppendingPathComponent:@"d_guzhang@3x.png"]];
    [paths addObject:[basePath stringByAppendingPathComponent:@"d_haha@2x.png"]];
    [paths addObject:[basePath stringByAppendingPathComponent:@"d_haixiu@3x.png"]];
    
    // 加載多圖
    UIImage *image = [[YYFrameImage alloc] initWithImagePaths:paths oneFrameDuration:0.1 loopCount:0];
    
    // 顯示多圖
    YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(30, 70, 50, 50);
    [self.view addSubview:imageView];
}

5 加載精靈圖片(使用YYSpriteSheetImage類)

    NSString *path = [[NSBundle mainBundle].bundlePath stringByAppendingPathComponent:@"ResourceTwitter.bundle/fav02l-sheet@2x.png"];
    UIImage *sheet = [[UIImage alloc] initWithData:[NSData dataWithContentsOfFile:path] scale:2];
    NSMutableArray *contentRects = [NSMutableArray new];
    NSMutableArray *durations = [NSMutableArray new];
    
    
    // 8 * 12 sprites in a single sheet image
    CGSize size = CGSizeMake(sheet.size.width / 8, sheet.size.height / 12);
    for (int j = 0; j < 12; j++) {
        for (int i = 0; i < 8; i++) {
            CGRect rect;
            rect.size = size;
            rect.origin.x = sheet.size.width / 8 * i;
            rect.origin.y = sheet.size.height / 12 * j;
            [contentRects addObject:[NSValue valueWithCGRect:rect]];
            [durations addObject:@(1 / 60.0)];
        }
    }
    YYSpriteSheetImage *sprite;
    sprite = [[YYSpriteSheetImage alloc] initWithSpriteSheetImage:sheet
                                                     contentRects:contentRects
                                                   frameDurations:durations
                                                        loopCount:0];
    
    
    YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:sprite];
    
    imageView.frame = CGRectMake(30, 70, 50, 50);
    
    [self.view addSubview:imageView];

總結(jié):由上面的例子可以看出,單張圖片的加載都是通過YYImage類完成,只有精靈圖片的加載(多個(gè)圖片元素在一張圖片上面),作者重新繼承了一個(gè)類YYSpriteSheetImage用于計(jì)算顯示的坐標(biāo)和時(shí)間等。

最后編輯于
?著作權(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)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,041評(píng)論 4 61
  • Swift版本點(diǎn)擊這里歡迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh閱讀 25,990評(píng)論 7 249
  • 能量總量2000卡 碳水:800蛋白質(zhì):800脂肪:200 早 1.牛奶一杯2.燕麥 353.水煮蛋 早中 1.橘...
    落花流水似無意閱讀 301評(píng)論 0 0
  • 解決方法:把composer.json中的"minimum-stability": "statable",改成"dev"
    朝文天下閱讀 396評(píng)論 0 0
  • 每天疲于奔命,沒有思想,只是往前走,不知道會(huì)遇到什么,也不知道會(huì)得到什么。只是全憑慣性,走呀走呀。 就這樣的不堪中...
    淺淺的柒閱讀 314評(píng)論 0 0

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