2010-02-10 73 views
20

我試圖保存一個UIIMage,然後檢索並顯示它。我已經成功地將圖像保存在包中,從那裏檢索並顯示。我的問題來自於當我嘗試將圖像保存到磁盤(將其轉換爲NSData),然後檢索它。我將圖像轉換爲NSData的是這樣的...NSData到UIImage

NSData* topImageData = UIImageJPEGRepresentation(topImage, 1.0); 

然後我把它寫像這樣的磁盤...

[topImageData writeToFile:topPathToFile atomically:NO]; 

然後我試圖找回它是這樣的...

topSubView.image = [[UIImage alloc] initWithContentsOfFile:topPathToFile]; 

它不返回圖像(大小爲零)。所以然後我試圖...

NSData *data = [[NSData alloc] initWithContentsOfFile:topPathToFile]; 
topSubView.image = [[UIImage alloc] initWithData:data]; 

無濟於事。當我通過調試器時,我確實看到數據包含正確的字節數,但我很困惑爲什麼我的圖像沒有被創建。我錯過了一步嗎?在轉換爲圖像之前,我需要使用NSData做些什麼?

回答

15

您應該能夠使用的UIImage類方法

[UIImage imageWithContentsOfFile:topPathToFile]; 

OR

[UIImage imageWithData:data]; 

難道這不是工作?

希望這會有所幫助!

+0

感謝您的答覆,但我得到了同樣的結果。仍然沒有圖像。我現在真的很難過。 – bruin 2010-02-11 05:14:01

33

試試看看這個代碼。這對我有效。

保存數據。

創建保存圖像的路徑。

let libraryDirectory = NSSearchPathForDirectoriesInDomains(.libraryDirectory, 
                   .userDomainMask, 
                   true)[0] 
let libraryURL = URL(fileURLWithPath: libraryDirectory, isDirectory: true) 
let fileURL = libraryURL.appendingPathComponent("image.data") 

將圖像轉換爲數據對象並將其保存到文件中。

let data = UIImageJPEGRepresentation(myImage, 1.0) 
try? data?.write(to: fileURL) 

檢索所保存的圖像

let newImage = UIImage(contentsOfFile: fileURL.relativePath) 

創建imageview的和與所檢索的圖像進行加載。

let imageView = UIImageView(image: newImage) 
self.view.addSubview(imageView) 
+0

圖像NSData爲我這樣工作:NSData * data = [NSData dataWithData:UIImagePNGRepresentation(myImage)]; – madmax 2011-05-29 14:55:27

+0

此NSData * data = UIImageJPEGRepresentation(myImage,1.0);是錯的。 – madmax 2011-05-29 14:56:10

+4

在說出某些錯誤之前,先嚐試一下代碼。 – ArunGJ 2011-05-29 16:49:46

6

爲了以防萬一,從iOS6開始,我們有imageWithData:scale方法。要從NSData對象中獲得具有正確比例的UIImage,請使用該方法,並聲明用於存儲原始圖像的比例。例如:

CGFloat screenScale = [[UIScreen mainScreen] scale]; 
UIImage *image = [UIImage imageWithData:myimage scale:screenScale]; 

這裏,myimage是您存儲原始圖像的NSData對象。在這個例子中,我使用了屏幕的比例。如果您對原始圖像使用其他比例,請改用該值。

0

使用帶數據的if-let塊來防止應用程序崩潰&安全執行代碼,因爲函數UIImagePNGRepresentation返回可選值。

if let img = UIImage(named: "TestImage.png") { 
    if let data:Data = UIImagePNGRepresentation(img) { 
     // Handle operations with data here...   
    } 
} 

注:數據是斯威夫特3+類。使用數據,而不是NSData的與 斯威夫特3+

通用圖像操作(如png格式& JPG兩種):

if let img = UIImage(named: "TestImage.png") { //UIImage(named: "TestImage.jpg") 
     if let data:Data = UIImagePNGRepresentation(img) { 
       handleOperationWithData(data: data)  
     } else if let data:Data = UIImageJPEGRepresentation(img, 1.0) { 
       handleOperationWithData(data: data)  
     } 
} 

******* 
func handleOperationWithData(data: Data) { 
    // Handle operations with data here... 
    if let image = UIImage(data: data) { 
     // Use image... 
    } 
} 

通過使用擴展:

extension UIImage { 

    var pngRepresentationData: Data? { 
     return UIImagePNGRepresentation(img) 
    } 

    var jpegRepresentationData: Data? { 
     return UIImageJPEGRepresentation(self, 1.0) 
    } 
} 

******* 
if let img = UIImage(named: "TestImage.png") { //UIImage(named: "TestImage.jpg") 
     if let data = img.pngRepresentationData { 
       handleOperationWithData(data: data)  
     } else if let data = img.jpegRepresentationData { 
       handleOperationWithData(data: data)  
    } 
} 

******* 
func handleOperationWithData(data: Data) { 
    // Handle operations with data here... 
    if let image = UIImage(data: data) { 
     // Use image... 
    } 
}