ios NSData 和 UIImage

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/257400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:21:56  来源:igfitidea点击:

NSData and UIImage

iosiphonecocoa-touchuiimagensdata

提问by BlueDolphin

I am trying to load UIImageobject from NSData, and the sample code was to NSImage, I guess they should be the same. But just now loading the image, I am wondering what's the best to troubleshoot the UIImageloading NSDataissue.

我正在尝试从 加载UIImage对象NSData,示例代码是到NSImage,我想它们应该是相同的。但是刚刚加载图像,我想知道解决UIImage加载NSData问题的最佳方法是什么。

采纳答案by Noah Witherspoon

UIImage has an -initWithData:method. From the docs: "The data in the data parameter must be formatted to match the file format of one of the system's supported image types."

UIImage 有一个 -initWithData:方法。来自文档:“数据参数中的数据必须格式化以匹配系统支持的图像类型之一的文件格式。”

回答by b123400

I didn't try UIImageJPEGRepresentation()before, but UIImagePNGRepresentationworks fine for me, and conversion between NSDataand UIImageis dead simple:

我之前没有尝试UIImageJPEGRepresentation()过,但UIImagePNGRepresentation对我来说效果很好,并且在NSData和之间的转换UIImage非常简单:

NSData *imageData = UIImagePNGRepresentation(image);
UIImage *image=[UIImage imageWithData:imageData];

回答by Noah Witherspoon

Try this to convert an image to NSdata:

试试这个将图像转换为 NSdata:

UIImage *img = [UIImage imageNamed:@"image.png"];
NSData *data1 = UIImagePNGRepresentation(img);

回答by leonho

theData should be a NSDataobject which already contains the data. You need to do the file loading/downloading to the NSDataobject before it is used. You can inspect it by using NSLogon theData and see if it contains the valid data.

theData 应该是一个NSData已经包含数据的对象。NSData在使用对象之前,您需要将文件加载/下载到对象。您可以通过NSLog在 theData 上使用来检查它并查看它是否包含有效数据。

回答by Krunal

For safe execution of code, use if-let block with Data, as function UIImagePNGRepresentation returns, optional value.

为了代码的安全执行,使用带有数据的 if-let 块,作为函数 UIImagePNGRepresentation 返回,可选值。

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

Note: Datais Swift 3 class. Use Data instead of NSData with Swift 3

注意:数据是 Swift 3 类。在 Swift 3 中使用 Data 而不是 NSData

Generic image operations (like png & jpg both):

通用图像操作(如 png 和 jpg 两者):

if let img = UIImage(named: "Hello.png") {
        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...
     }
}

By using extension:

通过使用扩展:

extension UIImage {

    var pngRepresentationData: Data? {
        return UIImagePNGRepresentation(img)
    }

    var jpegRepresentationData: Data? {
        return UIImageJPEGRepresentation(self, 1.0)
    }
}

*******
if let img = UIImage(named: "Hello.png") {
      if let data = img.pngRepresentationData {
              handleOperationWithData(data: data)     
      } else if let data = jpegRepresentationData {
              handleOperationWithData(data: data)     
     }
}

*******
func handleOperationWithData(data: Data) {
     // Handle operations with data here...
     if let image = UIImage(data: data) {
        // Use image...
     }
}