iOS图片处理
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS图片处理相关的知识,希望对你有一定的参考价值。
我们常用的图片格式可以分为bmp,jpg,png,gif,webp,其中bmp常用语安卓端开发,ios常用的是jpg和png,苹果默认是不支持gif图片的,我们拉取gif得到的是一帧帧的图片但是我们可以在工程中用代码生成gif图片,webp是google推出的一种新的图片格式,它的有点是可以将相同质量的图片大小缩减50%甚至更多,webp算法比较复杂,消耗内存较多,但基于其存储占用优势,以后可能会成为主流格式。
下边先从jpg和png谈起,iOS中我们常用png,因为清晰度相同的两张图片,png是无损的,所占空间更小。
一、png和jpg相互转化
//jpg转化png -(void)jpgToPng{ UIImage * image = [UIImage imageNamed:@"1.jpg"]; NSData * data = UIImagePNGRepresentation(image); UIImage * pngImage =[UIImage imageWithData:data]; UIImageWriteToSavedPhotosAlbum(pngImage, nil, nil, nil); } //jpg转化jpg -(void)jpgTojpg{ UIImage * image = [UIImage imageNamed:@"1.jpg"]; //后一个参数越小,则生成的图片越小,越模糊 NSData * data = UIImageJPEGRepresentation(image, 0.5); UIImage * jpgImage =[UIImage imageWithData:data]; UIImageWriteToSavedPhotosAlbum(jpgImage, nil, nil, nil); } //png转化png -(void)pngToJpg{ UIImage * image = [UIImage imageNamed:@"2.png"]; NSData * data = UIImageJPEGRepresentation(image, 0.5); UIImage * jpgImage =[UIImage imageWithData:data]; UIImageWriteToSavedPhotosAlbum(jpgImage, nil, nil, nil); }
二、gif图片分解
gif图片分解的步骤为
1.拿到gif数据
2.分帧
3.将单帧数据转化为图片
4.保存
首先我们需要先引入头文件
#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/MobileCoreServices.h>
//分解gif -(void)deCompositionGif{ //读取gif数据 NSString * gifResource = [[NSBundle mainBundle]pathForResource:@"1.gif" ofType:nil]; NSData * data = [NSData dataWithContentsOfFile:gifResource]; CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL); //获取gif帧数 size_t count = CGImageSourceGetCount(source); NSLog(@"%ld",count); //定义数组,保存单帧图片 NSMutableArray * ImageArr = [NSMutableArray array]; //遍历取出每一帧 for (size_t i = 0; i<count; i++) { CGImageRef imageRef = CGImageSourceCreateImageAtIndex(source, i, NULL); //将单帧数据转化为uiimag scale 图片物理尺寸与屏幕的比例 orientation 图片的方向 UIImage * image = [UIImage imageWithCGImage:imageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]; //保存 [ImageArr addObject:image]; //释放 CGImageRelease(imageRef); } CFRelease(source); //单帧图片保存 int i = 0 ; for (UIImage * image in ImageArr) { i++; NSData * data = UIImagePNGRepresentation(image); NSArray * path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString * gifPath = path[0]; NSString * pathNumber = [gifPath stringByAppendingString:[NSString stringWithFormat:@"%d.png",i]]; NSLog(@"%@",pathNumber); [data writeToFile:pathNumber atomically:YES]; } }
三.gif展示
UIImageView * imageView3 =[[UIImageView alloc]initWithFrame:CGRectMake(10, 100, 200, 200)]; [self.view addSubview:imageView3]; NSMutableArray * arr = [[NSMutableArray alloc]init]; for (int i = 1; i<9; i++) { UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:@"Documents%d",i]]; [arr addObject:image]; } //设置播放属性 [imageView3 setAnimationImages:arr]; [imageView3 setAnimationDuration:2]; [imageView3 setAnimationRepeatCount:10]; [imageView3 startAnimating];
四.由帧图片生成gif。
//创建gif图片 -(void)creatGif{ //获取gif数据 NSMutableArray * image =[[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"Documents1"],[UIImage imageNamed:@"Documents2"],[UIImage imageNamed:@"Documents3"],[UIImage imageNamed:@"Documents4"],[UIImage imageNamed:@"Documents5"],[UIImage imageNamed:@"Documents6"], nil]; //创建gif文件 NSArray * document = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentStr = [document objectAtIndex:0]; NSFileManager * manager = [NSFileManager defaultManager]; NSString * testDic = [documentStr stringByAppendingString:@"/gif"]; NSString * path = [testDic stringByAppendingString:@"test.gif"]; [manager createDirectoryAtPath:testDic withIntermediateDirectories:YES attributes:nil error:nil]; //配置属性 CGImageDestinationRef destion; //将path映射成 CFURLRef 对象 CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)path, kCFURLPOSIXPathStyle, false); //url 路径 kUTTypeGIF目标类型 destion = CGImageDestinationCreateWithURL(url, kUTTypeGIF, image.count, nil); NSDictionary * frameDic = [NSDictionary dictionaryWithObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:0.3], (NSString *)kCGImagePropertyGIFDelayTime,nil] forKey:(NSString *)kCGImagePropertyGIFDelayTime]; NSMutableDictionary * gifParmDic = [NSMutableDictionary dictionaryWithCapacity:3]; [gifParmDic setObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCGImagePropertyGIFHasGlobalColorMap]; [gifParmDic setObject:(NSString *)kCGImagePropertyColorModelRGB forKey:(NSString *)kCGImagePropertyColorModel]; [gifParmDic setObject:[NSNumber numberWithInt:8] forKey:(NSString *)kCGImagePropertyDepth]; [gifParmDic setObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount]; NSDictionary * gifProperty = [NSDictionary dictionaryWithObject:gifParmDic forKey:(NSString *)kCGImagePropertyGIFDictionary ]; //单帧图片添加到gif for (UIImage * Dimage in image) { CGImageDestinationAddImage(destion,Dimage.CGImage, (__bridge CFDictionaryRef)frameDic); } CGImageDestinationSetProperties(destion, (__bridge CFDictionaryRef)gifProperty); CGImageDestinationFinalize(destion); CFRelease(destion); }
以上是关于iOS图片处理的主要内容,如果未能解决你的问题,请参考以下文章