iOS中的图片

1、将图片保存到相册
<pre>
UIImage *image = [UIImage imageNamed:@"1.jpg"];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
</pre>

2、图片格式转化(JPG和PNG)
<pre>UIImagePNGRepresentation(image); //转化为png
UIImageJPEGRepresentation(image, 1); //转化为jpeg
最后1个参数:0-1,0压缩最大,质量差,1压缩最小,质量最好</pre>

3、将GIF图片分解为单帧,转化为UIImage保存到数组中

<pre>#import <ImageIO/ImageIO.h>

import <MobileCoreServices/MobileCoreServices.h><br />

NSString *gifPath = [[NSBundle mainBundle] pathForResource:@"123.gif" ofType:nil];
NSData *data = [NSData dataWithContentsOfFile:gifPath];
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL); // 获取数据源
size_t count = CGImageSourceGetCount(source);// 获取帧数
NSMutableArray *tempArray = [NSMutableArray array];
for (size_t i = 0; i < count; i++) {
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(source, i, NULL); // 获取单帧图片
UIImage *image = [UIImage imageWithCGImage:imageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
[tempArray addObject:image];
CGImageRelease(imageRef); // 释放单帧图片
}
CFRelease(source); // 释放数据源
</pre>

4、展示帧动画
<pre>
NSMutableArray *tempArray = [NSMutableArray array];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 375, 200)];
[self.view addSubview:imageView];
NSInteger pictureCount = 8;
for (int i = 0; i < pictureCount; i++) {
NSString *imageString = [NSString stringWithFormat:@"IMG_%02d.jpg",i];
NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageString ofType:nil];
// 注意此处不要使用[UIImage imageNamed:imageString]方法
// 使用上面的方法后会有内存缓存,内存飙升,容易导致程序闪退,优点是再次加载的时候直接从内存中取,速度很快
// imageWithContentsOfFile,不会有缓存
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
[tempArray addObject:image];
}

imageView.animationImages = tempArray; // 设置帧动画数组
imageView.animationRepeatCount = 1; // 设置动画重复次数
imageView.animationDuration = pictureCount * 0.1; // 设置动画持续时间

[imageView startAnimating];

// 在动画播放完成后清理内存
[imageView performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:5];
</pre>

5、用单帧图片合成gif图片
<pre>
// 1.获取图片数组
NSMutableArray *imageArray = [NSMutableArray array];
NSInteger pictureCount = 8;
for (int i=0; i < pictureCount; i++) {
NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d.png",i] ofType:nil];
UIImage *image = [UIImage imageWithContentsOfFile:path];
[imageArray addObject:image];
}
// 2.获取文件地址
NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *folder = [docPath stringByAppendingPathComponent:@"gif"];
[fileManager createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:nil error:NULL];
NSString *path = [folder stringByAppendingPathComponent:@"shark.gif"];
NSLog(@"path>>>>%@",path);

// 3.生成gif地址
CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)path, kCFURLPOSIXPathStyle, false);
CGImageDestinationRef destination = CGImageDestinationCreateWithURL(url, kUTTypeGIF, pictureCount, NULL);
for (UIImage *image in imageArray) {
CGImageDestinationAddImage(destination, image.CGImage, nil);
}
CGImageDestinationFinalize(destination);
CFRelease(destination);</pre>

6、关于蓝色文件夹和黄色文件夹
黄色文件夹可以直接访问,蓝色的文件夹的访问方式需要加相关路径
<pre>
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"picture/1.jpg" ofType:nil];
UIImage *image = [UIImage imageWithContentsOfFile:filePath];
或者
self.imageView.image = [UIImage imageNamed:@"picture/1.jpg"];
</pre>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 这是我的第一个项目,虽说是接手别人的,但是也很激动的说. 在项目中,需要改需求,要计算下载速度,其中用到了使用表盘...
    atiman阅读 11,058评论 0 2
  • 妈妈说,记得以后当了律师一定要感谢她。 初中,青春超级叛逆期。处于一个“我的青春我做主”的屌炸天的时期。那个时候,...
    一个好的坏人is阅读 1,267评论 0 0
  • 把你从回忆里剥离,一丝一缕。这抽出来的丝是我对你的爱与思念,我在没有你的日子里作茧自缚。这茧坚硬而缠绵,我出不来...
    Mar墨殇阅读 2,166评论 0 0
  • 估计大家都听过无数遍的一句话:“听过很多道理,依然过不好这一生。” 01 我也听过很多次,甚至还有室友问我,你觉得...
    笑自来阅读 3,964评论 6 4
  • 本文写给那些想极速集成推送功能的开发者,不用阅读任何文档,不用写一行代码,1分钟集成,立即使用! 不论你的项目是用...
    pikacode阅读 7,611评论 2 22