文章目录
  1. 1. 掌握的知识点
  2. 2. Jpg 和 Png 转化
  3. 3. GIF分解
  4. 4. GIF展示

掌握的知识点

  • UIImage 展示

    • Jpg Png Gif
  • UIImage 几何变换

    • 旋转
    • 拉伸
    • 裁剪
  • UIImage 动画
  • UIImage 内容层处理

Jpg 和 Png 转化

1
2
3
//写入本地相册
UIImage * image = [UIImage imageNamed:@"103-160Q509544OC.jpg"];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

jpgTopng

1
2
3
4
 UIImage * image = [UIImage imageNamed:@"123.jpg"];
NSData *data = UIImagePNGRepresentation(image);
UIImage * imagePng = [UIImage imageWithData:data];
UIImageWriteToSavedPhotosAlbum(imagePng, nil, nil, nil);

jpgToJpg

1
2
3
4
5
6
7
8
9
10
11
12
UIImage * image = [UIImage imageNamed:@"123.jpg"];
/**
* 图片转换成数据
*
* @param image 原始图片
* @param 0.4 质量因子
*
* @return
*/
NSData * data = UIImageJPEGRepresentation(image, 0.4);
UIImage *imageJpg = [UIImage imageWithData:data];
UIImageWriteToSavedPhotosAlbum(imageJpg, nil, nil, nil);

GIF分解

1.拿到GIF
2.将gif图片分解(decomposition)
3.将单帧转化为uiimage
4.将图片保存

注意:添加两个头文件

1
2
<MobileCoreServices/MobileCoreServices.h>
<ImageIO/ImageIO.h>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// 1.拿到GIF 
NSString * pathSource= [[NSBundle mainBundle]pathForResource:@"123" ofType:@"gif"];
NSData * data = [NSData dataWithContentsOfFile:pathSource];
CGImageSourceRef source =CGImageSourceCreateWithData((__bridge CFDataRef) data, NULL);
//2.将gif图片分解(decomposition)
size_t count = CGImageSourceGetCount(source);
NSMutableArray * tempArray = [NSMutableArray array];
for (int i = 0; i < count; i ++) {
CGImageRef imageCG = CGImageSourceCreateImageAtIndex(source, i, NULL);

//3.将单帧转化为uiimage
UIImage * image = [UIImage imageWithCGImage:imageCG scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
[tempArray addObject:image];
CGImageRelease(imageCG);
}
CFRelease(source);

//4.将图片保存
int i =0;
for (UIImage *image in tempArray) {
NSData * data = UIImagePNGRepresentation(image);
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * gifpath = path.firstObject;
NSString * pathNum = [gifpath stringByAppendingString:[NSString stringWithFormat:@"/%d.png",i]];
i++;
[data writeToFile:pathNum atomically:YES];
}

GIF展示

1
2
3
4
5
6
7
8
9
10
11
NSMutableArray * tempArray = [NSMutableArray array];
UIImageView * iamgeView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 100, 400, 220)];
[self.view addSubview:iamgeView];
for (int i = 0; i < 12; i++) {
UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:@"%d",i]];
[tempArray addObject:image];
}
[iamgeView setAnimationImages:tempArray];
[iamgeView setAnimationDuration:2];
[iamgeView setAnimationRepeatCount:10];
[iamgeView startAnimating];

未完待续。。。

文章目录
  1. 1. 掌握的知识点
  2. 2. Jpg 和 Png 转化
  3. 3. GIF分解
  4. 4. GIF展示