iphone 怎样将UIImage对象保存到JPG或者PNG文件中?
2012-03-13 21:35
573 查看
我们都知道如果要从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文件中?
- 将UIImage对象保存到JPG或者PNG文件中
- iPhone UIImage保存到文件,PNG/JPEG自适应
- iPhone UIImage保存到文件,PNG/JPEG自适应
- iPhone UIImage保存到文件,PNG/JPEG自适应
- iPhone UIImage保存到文件,PNG/JPEG自适应
- VC/MFC怎样将位图CBitmap对象保存成为bmp格式文件? from http://coding1688.iteye.com/blog/1547035
- Objective-C ,ios,iphone开发基础:ios判断图片为png还是jpg或者jepg
- easyWeb2Pic -- 在IE浏览器中,将整个网页保存为BMP, JPEG/JPG, GIF, PNG或者TIFF图片
- JPG,PNG等格式的图片转换的ICON保存为ICO文件
- VC/MFC怎样将位图CBitmap对象保存成为bmp格式文件?(你在网上找的不一定灵,看我的一定灵)
- 更新系统或者更换Eclipse后,双击Eclipse-drawable,或者在布局文件中,查看图片时,显示乱码。png 和 jpg
- VC屏幕截图并保存为bmp、jpg、png等格式文件
- 绘制bitmap图片保存,生成ico文件或者对象
- UIImage保存成png和jpg
- JPG,PNG等格式的图片转换的ICON保存为ICO文件
- iPhone UIImage保存到文件
- VC屏幕截图并保存为bmp、jpg、png等格式文件
- 怎样恢复丢失的JPG图片做好的图片文件忘记保存了怎么办