1.jpg-->png
- (void)jpgToPng{
UIImage * image = [UIImage imageNamed:@"faust_github.jpg"];
NSData * data = UIImagePNGRepresentation(image);
UIImage * imagePng = [UIImage imageWithData:data];
//图片保存到本地相册中
UIImageWriteToSavedPhotosAlbum(imagePng, nil, nil, nil);
}
2.jpg-->jpg
- (void)jpgToJpg{
UIImage * image = [UIImage imageNamed:@"faust_github.jpg"];
NSData * data = UIImageJPEGRepresentation(image, 1);//1是质量因子,越小,图片越小
UIImage * imagePng = [UIImage imageWithData:data];
//图片保存到本地相册中
UIImageWriteToSavedPhotosAlbum(imagePng, nil, nil, nil);
}
注:
- 1.imageNamed imageWithData
- 2.png无损,,jpg有损
3.播放gif图片
/**
* 1.我们要拿到gif数据
* 2.将gif分解一帧帧
* 3.将单帧数据转为UIImage
* 4.单帧图片保存
*/
- (void)viewDidLoad {
[super viewDidLoad];
[self deCompositionGif];
[self ShowGIf];
}
/**
* 展示gif
*/
- (void)ShowGIf{
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 270, 115)];
[self.view addSubview:imageView];
[imageView setAnimationImages:gifArray];
[imageView setAnimationRepeatCount:10];
[imageView setAnimationDuration:1];
[imageView startAnimating];
}
/**
* 分解gif
*/
- (void)deCompositionGif{
//1.我们要拿到gif数据
NSString * gifPathSource = [[NSBundle mainBundle] pathForResource:@"paopao" ofType:@"gif"];
NSData * data = [NSData dataWithContentsOfFile:gifPathSource];
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
//2.将gif分解一帧帧
size_t count = CGImageSourceGetCount(source);
NSMutableArray * tmpArray = [NSMutableArray arrayWithCapacity:0];
for (size_t i = 0; i < count; i++) {
CGImageRef imageref = CGImageSourceCreateImageAtIndex(source, i, NULL);
//3.将单帧数据转为UIImage
UIImage * image = [UIImage imageWithCGImage:imageref scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
[tmpArray addObject:image];
CGImageRelease(imageref);
}
CFRelease(source);
//4.单帧图片保存
int i = 0;
for (UIImage * img in tmpArray) {
NSData * data = UIImagePNGRepresentation(img);
NSArray * path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * gifPath = path[0];
NSLog(@"%@",gifPath);
NSString * pathNum = [gifPath stringByAppendingString:[NSString stringWithFormat:@"%d.png",i]];
i++;
[data writeToFile:pathNum atomically:YES];
}
gifArray = [NSMutableArray arrayWithArray:tmpArray];
}
4.png--->gif
- (void)createGif{
//1.获取我们的数据
NSMutableArray * pngArray = [NSMutableArray arrayWithCapacity:0];
for (int i = 1; i < 11; i++) {
UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:@"image%d.png",i]];
[pngArray addObject:image];
}
//2.创建gif文件
NSArray * document = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentStr = [document objectAtIndex:0];
NSFileManager * fileManager = [NSFileManager defaultManager];
NSString * textDic = [documentStr stringByAppendingString:@"/gif"];
[fileManager createDirectoryAtPath:textDic withIntermediateDirectories:YES attributes:nil error:nil];
NSString * path = [textDic stringByAppendingString:@"test1.gif"];
NSLog(@"%@",path);
//3.配置gif属性
CGImageDestinationRef destion;
CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)path, kCFURLPOSIXPathStyle, false);
CGImageDestinationCreateWithURL(url, kUTTypeGIF, pngArray.count, NULL);
NSDictionary * frameDic = [NSDictionary dictionaryWithObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:0.3], (NSString *)kCGImagePropertyGIFDelayTime, nil] forKey:(NSString *)kCGImagePropertyGIFDelayTime];
NSMutableDictionary * gifParaDict = [NSMutableDictionary dictionaryWithCapacity:0];
[gifParaDict setObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCGImagePropertyGIFHasGlobalColorMap];
[gifParaDict setObject:(NSString *)kCGImagePropertyColorModelRGB forKey:(NSString *)kCGImagePropertyColorModel];
[gifParaDict setObject:[NSNumber numberWithInt:8] forKey:(NSString *)kCGImagePropertyDepth];
[gifParaDict setObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount];
NSDictionary * gifProperty = [NSDictionary dictionaryWithObject:gifParaDict forKey:(NSString *)kCGImagePropertyGIFDictionary];
//4.单帧添加到gif
for (UIImage * image in pngArray) {
CGImageDestinationAddImage(destion, image.CGImage, (__bridge CFDictionaryRef) frameDic);
}
CGImageDestinationSetProperties(destion, (__bridge CFDictionaryRef)gifProperty);
CGImageDestinationFinalize(destion);
CFRelease(destion);
}