from:http://blog.csdn.net/wherejaly/article/details/5538248
如果要从data中或者file中读取数据并包装成UIImage可以使用+ p_w_picpathWithData: 和+ p_w_picpathWithContentsOfFile: 但如果想把UIImage的图片数据写入到jpg或者png格式的文件中呢?答案是UIImageJPEGRepresentation,请看如下代码
/ Create paths to output p_w_picpaths
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 'p_w_picpath' must be a UIImage object
// The value '1.0' represents p_w_picpath compression quality as value from 0.0 to 1.0
[UIImageJPEGRepresentation(p_w_picpath, 1.0) writeToFile:jpgPath atomically:YES];
// Write p_w_picpath to PNG
[UIImagePNGRepresentation(p_w_picpath) 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]);