springlo 发表于 2012-12-19 22:18:04

将UIImage对象保存为PNG或JPEG文件

<div id="cnblogs_post_body">获取UIImage的数据,PNG或JPEG

UIKit中包含两个C函数,UIImageJPEGRepresentation和UIImagePNGRepresentation将返回一个NSData的对象,它代表一个JPEG或PNG格式的图像。有了这些信息在手,你就可以使用NSData的对象将writeToFile方法的图像数据写入到指定的路径。
<div class="wp_syntax" style="font: 13px/normal 'Lucida Grande', Arial, Helvetica, sans-serif; margin: 0px 0px 1.5em; padding: 0px; border: 1px solid silver; width: 603.9px; text-align: left; color: #110000; text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; overflow-x: auto; overflow-y: hidden; orphans: 2; widows: 2; font-size-adjust: none; font-stretch: normal; background-color: #f9f9f9; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">1234567891011121314151617181920212223// Create paths to output imagesNSString*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 managerNSError *error;NSFileManager *fileMgr = [NSFileManager defaultManager]; // Point to Document directoryNSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; // Write out the contents of home directory to consoleNSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);
页: [1]
查看完整版本: 将UIImage对象保存为PNG或JPEG文件