繁体   English   中英

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

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

我正在制作一个iOS应用程序,并且有一个UIImage-我想将其压缩为.png文件并将其保存到应用程序的documents文件夹中-我已经有了路径,我所需要的只是如何将UIImage转换为.png并保存它。

谢谢,马坦。

所以代码是:

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

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

对于PNG:

UIImagePNGRepresentation

以PNG格式返回指定图像的数据

NSData * UIImagePNGRepresentation (
   UIImage *image
);

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

UIImageJPEGRepresentation

以JPEG格式返回指定图像的数据。

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

图像压缩格式为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