UIImage保存成png和jpg

from:http://blog.csdn.net/wherejaly/article/details/5538248app

若是要從data中或者file中讀取數據幷包裝成UIImage可使用+ p_w_picpathWithData: 和+ p_w_picpathWithContentsOfFile: 但若是想把UIImage的圖片數據寫入到jpg或者png格式的文件中呢?答案是UIImageJPEGRepresentation,請看以下代碼ide


  1. / Create paths to output p_w_picpaths atom

  2. NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.png"]; url

  3. NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"]; spa

  4. // Write a UIImage to JPEG with minimum compression (best quality).net

  5. // The value 'p_w_picpath' must be a UIImage objectorm

  6. // The value '1.0' represents p_w_picpath compression quality as value from 0.0 to 1.0blog

  7. [UIImageJPEGRepresentation(p_w_picpath, 1.0) writeToFile:jpgPath atomically:YES]; 圖片

  8. // Write p_w_picpath to PNGip

  9. [UIImagePNGRepresentation(p_w_picpath) writeToFile:pngPath atomically:YES];

  10. // Let's check to see if files were successfully written...

  11. // Create file manager

  12. NSError *error;

  13. NSFileManager *fileMgr = [NSFileManager defaultManager];

  14. // Point to Document directory

  15. NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

  16. // Write out the contents of home directory to console

  17. NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);

相關文章
相關標籤/搜索