本文主要是介绍iphone 怎样将UIImage对象保存到JPG或者PNG文件中?,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
我们都知道如果要从data中或者file中读取数据并包装成UIImage可以使用+ imageWithData: 和+ imageWithContentsOfFile: 但如果想把UIImage的图片数据写入到jpg或者png格式的文件中呢?答案是UIImageJPEGRepresentation,请看如下代码
- / Create paths to output images
- NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.png"];
- NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"];
- // Write a UIImage to JPEG with minimum compression (best quality)
- // The value 'image' must be a UIImage object
- // The value '1.0' represents image compression quality as value from 0.0 to 1.0
- [UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];
- // Write image to PNG
- [UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];
- // Let's check to see if files were successfully written...
- // Create file manager
- NSError *error;
- NSFileManager *fileMgr = [NSFileManager defaultManager];
- // Point to Document directory
- NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
- // Write out the contents of home directory to console
- NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);
这篇关于iphone 怎样将UIImage对象保存到JPG或者PNG文件中?的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!