简体   繁体   English

Xcode iOS生成.png文件并使用NSFileManager保存

[英]Xcode iOS Generate .png file and save it using NSFileManager

I am making an iOS app and I got a UIImage - I want to compress it into .png file and save it to the app's documents folder - I already have the path and all I need is how to convert the UIImage to .png and save it. 我正在制作一个iOS应用程序,并且有一个UIImage-我想将其压缩为.png文件并将其保存到应用程序的documents文件夹中-我已经有了路径,我所需要的只是如何将UIImage转换为.png并保存它。

Thanks, Matan. 谢谢,马坦。

so the code is: 所以代码是:

UIImage *yourImage = ...; //the image you have
NSString *targetPath = ...; // something like ~/Library/Documents/myImage.png

[UIImagePNGRepresentation(yourImage) writeToFile:targetPath atomically:YES];

For PNG: 对于PNG:

UIImagePNGRepresentation

Returns the data for the specified image in PNG format 以PNG格式返回指定图像的数据

NSData * UIImagePNGRepresentation (
   UIImage *image
);

If you wanted JPEG instead: 如果您要使用JPEG,请执行以下操作:

UIImageJPEGRepresentation

Returns the data for the specified image in JPEG format. 以JPEG格式返回指定图像的数据。

NSData * UIImageJPEGRepresentation (
   UIImage *image,
   CGFloat compressionQuality
);

Image compression form is JPEG you can use different quality of jpg image 图像压缩格式为JPEG,可以使用不同质量的jpg图像

// for getting png image
 NSData*theImageData=UIImagePNGRepresentation(theImage);
// for JPG compression change fill value between less than 1 
 NSData*theImageData=UIImageJPEGRepresentation(theImage, 1);
// for converting raw data image to UIImage
 UIImage *imageOrignal=[UIImage imageWithData:theImageData];
// for saving in to photos gallery
 UIImageWriteToSavedPhotosAlbum(imageOrignal,nil,nil,nil);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM