2011-05-31 74 views
1

我在應用程序中使用Coredata保存圖像。然後,我將Sqlite文件導入到另一個項目中,並檢索到該Blob字段並轉換爲NSData。然後當我嘗試將這個NSData轉換爲UIImage時,我得到了一個N​​ULL值。從Sqlite數據庫將Blob轉換爲UIImage

NSData *data=UIImageJPEGRepresentation(tempImage, 1.0); 

通過保存上下文

從sqlite的回來::

const char *sqlStatement = "select ID,NAME,IMAGE from CALL"; 
sqlite3_stmt *compiledStatement; 
NSData *data=[[NSData alloc] initWithBytes: sqlite3_column_blob(compiledStatement, 2) length:sqlite3_column_bytes(compiledStatement, 2)]; 
UIImage * showImage= [UIImage imageWithData: data]; 

回答

1

你正在寫的代碼是正確的保存上述數據。但是,您可以按照this tutorial的說明操作,其中顯示瞭如何將圖像存儲到數據庫中並從數據庫中檢索圖像。

差我在你的代碼中發現和他們的代碼是

>-NSData *data=UIImageJPEGRepresentation(tempImage, 1.0); //You use UIImageJPEGRepresentation 

>-NSData *imgData = UIImagePNGRepresentation(self.coffeeImage);//You use UIImagePNGRepresentation 

雖然這不是你的問題的正確解決方案,但儘管它可以幫助你在問題進一步調查。

希望它有幫助。