打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
怎样将UIImage对象保存到JPG或者PNG文件中?
我们都知道如果要从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]);
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
IOS开发之细节知识点汇总
专门恢复误删的JPG、Gif、PNG图片
IOS的Bundle资源束制作
iOS开发图片加载的内存问题及优化方案
IOS开发中@2x图片等适应不同分辨率手机
更改文件夹的背景
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服