NSData到UIImage

问题描述 投票:20回答:5

我正在尝试保存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执行某些操作?

iphone uiimage nsdata
5个回答
33
投票

试试这个代码。这对我有用。

保存数据。

创建保存图像的路径。

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)

17
投票

您应该能够使用UIImage的类方法

[UIImage imageWithContentsOfFile:topPathToFile]; 

要么

[UIImage imageWithData:data];

这不起作用吗?

希望这可以帮助!


6
投票

为了防止有人,从iOS6我们有了imageWithData:scale方法。要从NSData对象获取具有正确比例的UIImage,请使用该方法,声明用于存储原始图像的比例。例如:

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

这里,myimage是存储原始图像的NSData对象。在示例中,我使用了屏幕的比例。如果您对原始图像使用其他比例,请改用该值。


0
投票

看看这个:http://www.nixwire.com/getting-uiimage-to-work-with-nscoding-encodewithcoder/

它正是我认为你的问题的解决方案。你不能只用NSData制作图像,即使这是常识所暗示的。你必须使用UIImagePNGRepresentation。如果这有帮助,请告诉我。


0
投票

使用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...
     }
}
© www.soinside.com 2019 - 2024. All rights reserved.