将UIImage转换为NSData并返回UIImage

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将UIImage转换为NSData并返回UIImage相关的知识,希望对你有一定的参考价值。

我必须从数据库中填充表格单元格,并且图像在设备上。我要将这个图像存储在imageDataObject中,如何将UIImage转换为NSData。请建议,我尝试了很多解决方案,但它没有将NSData转换回UIImage

    cell.textLabel.text = [[offlineImageListArray objectAtIndex:indexPath.row] imageName];
    UIImage *thumbnail = [self retrieveImageFromDevice:[[offlineImageListArray objectAtIndex:indexPath.row] imageName]];
    NSData* data = ??????????;
    ImageData *imageDataObject = [[ImageData alloc] initWithImageId:[[offlineImageListArray objectAtIndex:indexPath.row]imageId] 
    imageName:[[offlineImageListArray objectAtIndex:indexPath.row] imageName] imageData:data];
    [imagesArray addObject:imageDataObject];
答案

要将UIImage转换为NSData,请使用UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality)UIImagePNGRepresentation(UIImage *image)

要将NSData转换为UIImage,请使用[UIImage imageWithData:imageData]

所以你的例子看起来像这样:

cell.textLabel.text = [[offlineImageListArray objectAtIndex:indexPath.row] imageName];
UIImage *thumbnail = [self retrieveImageFromDevice:[[offlineImageListArray objectAtIndex:indexPath.row] imageName]];
NSData* data = UIImagePNGRepresentation(thumbnail);
ImageData *imageDataObject = [[ImageData alloc] initWithImageId:[[offlineImageListArray objectAtIndex:indexPath.row]imageId] imageName:[[offlineImageListArray objectAtIndex:indexPath.row] imageName] imageData:data];
[imagesArray addObject:imageDataObject];

参考文献:https://developer.apple.com/LIBRARY/IOS/documentation/UIKit/Reference/UIKitFunctionReference/Reference/reference.html https://developer.apple.com/Library/ios/documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html

另一答案

试试这个:

用它来将UIImage转换为NSData

  NSData *imagedata = UIImagePNGRepresentation(thumbnail);

尝试将图像数据转换回UIImage

  UIImage *image= [UIImage imageWithData:imagedata];
另一答案

在Swift版本中:

    let imageData = UIImagePNGRepresentation(image!)!
    let imageFromData = UIImage(data: imageData)
另一答案

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

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

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

通用图像操作(如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...
     }
}
另一答案

从图像转换数据:

请尝试JPEG或PNG表示,具体取决于您的图像格式:

UIImageJPEGRepresentation

以JPEG格式返回指定图像的数据。

NSData * UIImageJPEGRepresentation (
   UIImage *image,
   CGFloat compressionQuality
);

UIImagePNGRepresentation

以PNG格式返回指定图像的数据

NSData * UIImagePNGRepresentation (
   UIImage *image
);

例:

extension UIImage {

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

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

使用:

let data = myImage.jpegRepresentationData
let data1 = myImage.pngRepresentationData

从数据转换图像:

let image = UIImage(data: data)

以上是关于将UIImage转换为NSData并返回UIImage的主要内容,如果未能解决你的问题,请参考以下文章

使用正确的类型将 NSValue 转换为 NSData 并再次返回

如何处理 NSData 并返回 UIImage (Swift)

将 UIImage 转换为 NSData 并在 Swift 中转换回 UIImage?

将UIImage转换为NSData并在Swift中转换回UIImage?

NSData到UIImage

将UIImage转换为数据URI