【问题标题】:UIImagePNGRepresentation(UIImage()) returns nilUIImagePNGRepresentation(UIImage()) 返回 nil
【发布时间】:2015-05-04 23:36:43
【问题描述】:

为什么UIImagePNGRepresentation(UIImage()) 返回nil

我试图在我的测试代码中创建一个UIImage(),只是为了断言它被正确传递。

我对两个 UIImage 的比较方法使用了UIImagePNGRepresentation(),但由于某种原因,它返回了nil

谢谢。

【问题讨论】:

  • 您实际上是在尝试使用UIImagePNGRepresentation(UIImage()),还是UIImage() 只是已经创建的UIImage 的占位符?
  • 我有一个传递的结构,该结构包含一个 UIImage。我将其属性设置为= UIImage()。然后我在这个属性和其他一些 UIImage 之间进行比较。此比较是通过UIImagePNGRepresentation(image1) == UIImagePNGRepresentation(image2) 完成的

标签: swift uiimage uiimagepngrepresentation


【解决方案1】:

我在将 UIImage 转换为 pngData 时遇到了同样的问题,但有时它返回 nil。我通过创建图像副本来修复它

       func getImagePngData(img : UIImage) -> Data {

            let pngData = Data()

            if let hasData = img.pngData(){
                print(hasData)
                pngData = hasData
             }
            else{
                    UIGraphicsBeginImageContext(img.size)
                    img.draw(in: CGRect(x: 0.0, y: 0.0, width: img.width, 
                    height: img.height))
                    let resultImage =
                                   UIGraphicsGetImageFromCurrentImageContext()
                    UIGraphicsEndImageContext()
                    print(resultImage.pngData)
                    pngData = resultImage.pngData
            }
         return pngData
        }

【讨论】:

  • 你如何运行这段代码? pngData 是一个 let,UIImage 上没有高度或宽度...
【解决方案2】:

如果提供的 UIImage 不包含任何数据,UIImagePNGRepresentation() 将返回 nil。来自UIKit Documentation

返回值

包含 PNG 数据的数据对象,如果生成数据时出现问题,则返回 nil。 如果图像没有数据或底层 CGImageRef 包含不受支持的位图格式的数据,此函数可能会返回 nil。

当您通过简单地使用UIImage() 来初始化UIImage 时,它会创建一个没有数据的UIImage。尽管图像不是零,但它仍然没有数据。而且,由于图像没有数据,UIImagePNGRepresentation() 只返回nil

要解决此问题,您必须将UIImage 与数据一起使用。例如:

var imageName: String = "MyImageName.png"
var image = UIImage(named: imageName)
var rep = UIImagePNGRepresentation(image)

imageName 是您的图像的名称,包含在您的应用程序中。

为了使用UIImagePNGRepresentation(image)image不能是nil,并且它还必须有数据。

如果你想检查他们是否有任何数据,你可以使用:

if(image == nil || image == UIImage()){
  //image is nil, or has no data
}
else{
  //image has data
}

【讨论】:

  • 如何查看图片是否有数据?因为UIImagePNGRepresentation 如果通过UIImage() 只会使应用程序崩溃...
  • 这不起作用,当我尝试比较 UIImagePNGRepresentation 的返回值时,应用程序崩溃了。
  • 如果您使用的是这个确切的代码,那么它将不起作用,因为您的应用程序中没有包含名为 MyImageName.png 的图像。这只是你可以做的一个例子。 TL;DR; 您必须以某种方式创建一个包含一定数量数据的 UIImage,以允许 UIImagePNGRepresentation(image) 不返回 nil。你不能使用UIImage() 作为你的图片,因为它没有数据。
  • 但是如果传递给 UIImagePNGRepresentation 的图像有任何数据,我如何检查我的Comparison 方法?所以如果 image1 或 image2 没有数据,我可以避免做UIImagePNGRepresentation(image1) == UIImagePNGRepresentation(image2)(这会崩溃)。
  • 我刚刚更新了我的答案,如果你想检查他们是否有任何数据,你可以检查是否image == UIImage(),它本质上检查图像是否等于没有数据的图像跨度>
【解决方案3】:

UIImage documentation

图像对象是不可变的,因此您无法在创建后更改它们的属性。这意味着您通常在初始化时指定图像的属性或依赖图像的元数据来提供属性值。

由于您在没有提供任何图像数据的情况下创建了UIImage,因此您创建的对象作为图像没有任何意义。 UIKit 和 Core Graphics 似乎不允许 0x0 图像。

最简单的解决方法是改为创建 1x1 图像:

UIGraphicsBeginImageContext(CGSizeMake(1, 1))
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-20
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多