NSData 和 UIImage

发布于 2024-07-08 10:37:09 字数 171 浏览 10 评论 0原文

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

I am trying to load UIImage object 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 UIImage loading NSData issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

风筝有风,海豚有海 2024-07-15 10:37:09

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

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

I didn't try UIImageJPEGRepresentation() before, but UIImagePNGRepresentation works fine for me, and conversion between NSData and UIImage is dead simple:

NSData *imageData = UIImagePNGRepresentation(image);
UIImage *image=[UIImage imageWithData:imageData];
西瑶 2024-07-15 10:37:09

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

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."

如日中天 2024-07-15 10:37:09

尝试将图像转换为 NSdata:

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

Try this to convert an image to NSdata:

UIImage *img = [UIImage imageNamed:@"image.png"];
NSData *data1 = UIImagePNGRepresentation(img);
枯叶蝶 2024-07-15 10:37:09

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

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

守不住的情 2024-07-15 10:37:09

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

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

通过使用扩展:

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...
     }
}

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

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

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

Generic image operations (like png & jpg both):

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...
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文