【问题标题】:UIImagePNGRepresentation returns nil data?UIImagePNGRepresentation 返回零数据?
【发布时间】:2013-07-03 06:00:43
【问题描述】:

我正在尝试制作缩略图并保存到文档目录。 但问题是,当我尝试将缩略图图像转换为 NSData 时。它返回 nil。

这是我的代码,

  UIImage *thumbNailimage=[image thumbnailImage:40 transparentBorder:0.2 cornerRadius:0.2 interpolationQuality:1.0];
NSData *thumbNailimageData = UIImagePNGRepresentation(thumbNailimage);// Returns nil
[thumbNailimageData writeToFile:[DOCUMENTPATH stringByAppendingPathComponent:@"1.png"] atomically:NO];

那么我也尝试过 UIImageJPEGRepresentation 的问题是什么,但它对我不起作用。

谢谢。

【问题讨论】:

  • 你有没有追踪过你在 thumbNailimage 中得到的图像...?
  • thumbNailimage 可能也是nil
  • 尝试使用一个显示 thumbNailimage 对象的图像视图并确认该图像是否正确。
  • 不,我得到了 thumbNailimage 对象,它不是 nil..

标签: ios objective-c xcode


【解决方案1】:

试试这个:

UIGraphicsBeginImageContext(originalImage.size);
[originalImage drawInRect:CGRectMake(0, 0, originalImage.size.width, originalImage.size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

这会创建原始 UIImage 的副本。然后您可以拨打UIImagePNGRepresentation,它会正常工作。

【讨论】:

    【解决方案2】:

    试试这个代码,

    -(void) createThumbnail
    {
       UIImage *originalImage = imgView2.image; // Give your original Image
       CGSize destinationSize = CGSizeMake(25, 25); // Give your Desired thumbnail Size
       UIGraphicsBeginImageContext(destinationSize);
       [originalImage drawInRect:CGRectMake(0,0,destinationSize.width,destinationSize.height)];
       UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
       NSData *thumbNailimageData = UIImagePNGRepresentation(newImage);
       UIGraphicsEndImageContext();
       [thumbNailimageData writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:@"1.png"] atomically:NO];
    }
    

    希望对你有帮助, 快乐编码

    【讨论】:

      【解决方案3】:

      对于 Swift 程序员,Rickster 的回答对我帮助很大! UIImageJPEGRepresentation 在选择某个图像时使我的应用程序崩溃。我正在分享我的 UIImage 扩展(或 Objective-C 术语中的类别)。

      import UIKit
      
      extension UIImage {
      
          /**
           Creates the UIImageJPEGRepresentation out of an UIImage
           @return Data
           */
      
          func generateJPEGRepresentation() -> Data {
      
              let newImage = self.copyOriginalImage()
              let newData = UIImageJPEGRepresentation(newImage, 0.75)
      
              return newData!
          }
      
          /**
           Copies Original Image which fixes the crash for extracting Data from UIImage
           @return UIImage
           */
      
          private func copyOriginalImage() -> UIImage {
              UIGraphicsBeginImageContext(self.size);
              self.draw(in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))
              let newImage = UIGraphicsGetImageFromCurrentImageContext()
              UIGraphicsEndImageContext();
      
              return newImage!
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2015-05-04
        • 2011-06-05
        • 2013-10-17
        • 1970-01-01
        • 2020-01-20
        • 2015-04-01
        • 2010-10-29
        • 2018-06-18
        • 2021-08-12
        相关资源
        最近更新 更多