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í)間等砸西。