UIImage保存成png和jpg
2013-09-16 09:35
381 查看
from:http://blog.csdn.net/wherejaly/article/details/5538248如果要从data中或者file中读取数据并包装成UIImage可以使用+ imageWithData: 和+ imageWithContentsOfFile: 但如果想把UIImage的图片数据写入到jpg或者png格式的文件中呢?答案是UIImageJPEGRepresentation,请看如下代码
[cpp]view plaincopy/ 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]);
[cpp]view plaincopy/ 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]);
相关文章推荐
- 怎样将UIImage对象保存到JPG或者PNG文件中?
- 怎样将UIImage对象保存到JPG或者PNG文件中?
- iphone 怎样将UIImage对象保存到JPG或者PNG文件中?
- 将UIImage对象保存到JPG或者PNG文件中
- iPhone UIImage保存到文件,PNG/JPEG自适应
- 【转】前端图片该保存为什么格式?png or jpg?
- iPhone UIImage保存到文件,PNG/JPEG自适应
- 将UIView转成UIImage,将UIImage转成PNG/JPG
- php保存远程图片改变尺寸,JPG转PNG,裁剪圆形
- IOS图像2之jpg、png图片保存、互转、设置有损压缩因子
- 将UIView转成UIImage,将UIImage转成PNG/JPG
- VB6.0用GDI+保存图片为JPG、TIFF、PNG、GIF、BMP等格式
- VB6.0用GDI+保存图像为BMP\JPG\PNG\GIF格式终结版。
- JPG,PNG等格式的图片转换的ICON保存为ICO文件
- iOS_UIImage_jpg<-->png转换
- VC屏幕截图并保存为bmp、jpg、png等格式文件
- flex 保存控件为jpg或png
- VC屏幕截图并保存为bmp、jpg、png等格式文件
- iPhone UIImage保存到文件,PNG/JPEG自适应
- 数字图像处理 CImage类的使用与封装(jpg png gif tif bmp等格式图像的加载、数据读写、保存等功能)