2013-04-26 58 views
0

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

谢谢, 马坦。

回答

5

因此代码:

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

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

谢谢你非常非常感谢!完美的作品! – Matan 2013-04-26 08:09:47

+0

是否有任何方法来压缩PNG? – 2013-12-18 05:13:54

+0

没有。你可以包含pngcrush来源。但框架中没有任何内容 – 2013-12-18 07:44:56

3

对于PNG:

UIImagePNGRepresentation 

返回数据为PNG格式

NSData * UIImagePNGRepresentation (
    UIImage *image 
); 

指定的图像。如果你想JPEG代替:

UIImageJPEGRepresentation 

返回数据为指定的图像采用JPEG格式。

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

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