【问题标题】:NSData to UIImageNSData 到 UIImage
【发布时间】:2010-02-10 22:15:49
【问题描述】:

我正在尝试保存 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


    【解决方案1】:

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

    保存数据。

    创建保存图像的路径。

    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)
    

    创建一个图像视图并使用检索到的图像加载它。

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

    【讨论】:

    • 图像到 NSData 像这样为我工作: NSData *data = [NSData dataWithData:UIImagePNGRepresentation(myImage)];
    • 这个 NSData* 数据 = UIImageJPEGRepresentation(myImage, 1.0);错了。
    • 是的,我试过了,但它没有为我创建数据。我认为这是分配 NSData 对象的正确方法。
    • 我在发布之前尝试了代码。它对我来说 100% 有效。要创建 nsdata 对象,有多种方法。你不能说有些是错的。
    • 对我来说唯一的问题是 UIImageJPEGRepresentation 太慢了。 1.5 MB 图片需要 4-5 秒。
    【解决方案2】:

    你应该可以使用 UIImage 的类方法

    [UIImage imageWithContentsOfFile:topPathToFile]; 
    

    [UIImage imageWithData:data];
    

    这没有用吗?

    希望这会有所帮助!

    【讨论】:

    • 感谢您的回复,但我得到了相同的结果。仍然没有图像。我现在真的很难过。
    【解决方案3】:

    以防万一这对某人有帮助,从 iOS6 开始,我们有 imageWithData:scale 方法。要从 NSData 对象获取具有正确比例的 UIImage,请使用该方法,声明用于存储原始图像的比例。例如:

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

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

    【讨论】:

      【解决方案4】:

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

      它正是我认为解决您问题的方法。你不能只用 NSData 制作图像,即使这是常识所暗示的。你必须使用 UIImagePNGRepresentation。让我知道这是否有帮助。

      【讨论】:

        【解决方案5】:

        使用带有数据的 if-let 块来防止应用程序崩溃和安全执行代码,因为函数 UIImagePNGRepresentation 返回一个可选值。

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

        注意:Data 是 Swift 3+ 类。使用 Data 而不是 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...
             }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-08-21
          • 2017-03-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-06-09
          • 2010-09-20
          相关资源
          最近更新 更多