簡體   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