8

在我的iPhone應用程序中,我從iPhone圖像庫以及設備相機 中選取圖像,並在imageView中顯示該圖像。iPhone將圖像從圖像存儲到文件

我能夠將我的圖像存儲到iPhone圖像庫。

但是現在我想將我的圖像存儲在某個具有特定名稱的目錄中,因此我可以在我的應用程序中再次使用該圖像,並且還希望將其存儲在sqlite文件中。

回答

9

有這樣一個簡短的書面記錄在這裏:http://iosdevelopertips.com/data-file-management/save-uiimage-object-as-a-png-or-jpeg-file.html

這裏是直接從該網站竊取相關代碼:

// Create paths to output images 
NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.png"]; 
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"]; 

// Write a UIImage to JPEG with minimum compression (best quality) 
// The value 'image' must be a UIImage object 
// The value '1.0' represents image compression quality as value from 0.0 to 1.0 
[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES]; 

// Write image to PNG 
[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES]; 

將其存儲在一個SQLite數據庫,你會採取使代碼NSData對象(UIImageJPEGRepresentationUIImagePNGRepresentation)並將它們保存到BLOB列。

+0

iphonesdkarticles.com已經死了,robots.txt禁止archive.org抓取它。 – 2017-07-01 05:06:53

3
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/ImageFileName.jpg"]; 
[UIImageJPEGRepresentation(yourUIImageView.image,1.0) writeToFile:path atomically:YES]; 

文檔:UIImageJPEGRepresentation

如果你正在處理的PNG有一個名爲UIImagePNGRepresentation另一種方法。