[英]NSData to UIImage
我正在嘗試保存UIIMage,然后檢索並顯示它。 通過在圖像包中保存圖像,從那里檢索並顯示圖像,我獲得了成功。 我的問題來自於我嘗試將圖像保存到磁盤(將其轉換為NSData),然后檢索它。 我像這樣將圖像轉換為NSData ...
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執行某些操作?
試試這個代碼。 這對我有用。
保存數據。
創建保存圖像的路徑。
let libraryDirectory = NSSearchPathForDirectoriesInDomains(.libraryDirectory,
.userDomainMask,
true)[0]
let libraryURL = URL(fileURLWithPath: libraryDirectory, isDirectory: true)
let fileURL = libraryURL.appendingPathComponent("image.data")
將圖像轉換為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)
您應該能夠使用UIImage的類方法
[UIImage imageWithContentsOfFile:topPathToFile];
要么
[UIImage imageWithData:data];
這不起作用嗎?
希望這可以幫助!
為了防止有人,從iOS6我們有了imageWithData:scale方法。 要從NSData對象獲取具有正確比例的UIImage,請使用該方法,聲明用於存儲原始圖像的比例。 例如:
CGFloat screenScale = [[UIScreen mainScreen] scale];
UIImage *image = [UIImage imageWithData:myimage scale:screenScale];
這里,myimage是存儲原始圖像的NSData對象。 在示例中,我使用了屏幕的比例。 如果您對原始圖像使用其他比例,請改用該值。
使用if-let阻止數據以防止應用程序崩潰和安全執行代碼,因為函數UIImagePNGRepresentation返回一個可選值。
if let img = UIImage(named: "TestImage.png") {
if let data:Data = UIImagePNGRepresentation(img) {
// Handle operations with data here...
}
}
注意: 數據是Swift 3+類。 使用數據代替NSData和Swift 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...
}
}
看看這個: http : //www.nixwire.com/getting-uiimage-to-work-with-nscoding-encodewithcoder/ 。
它正是我認為你的問題的解決方案。 你不能只用NSData制作圖像,即使這是常識所暗示的。 你必須使用UIImagePNGRepresentation。 如果這有幫助,請告訴我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.