将UIImage转换为NSData并返回UIImage

时间:2013-11-16 00:40:32

标签: ios uiimage nsdata

我必须从数据库中填充表格单元格,并且图像在设备上。 我要将该图片存储在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];

5 个答案:

答案 0 :(得分:18)

要将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

答案 1 :(得分:9)

试试这个:

使用此功能将UIImage转换为NSData

  NSData *imagedata = UIImagePNGRepresentation(thumbnail);

尝试将图片数据转换回UIImage

  UIImage *image= [UIImage imageWithData:imagedata];

答案 2 :(得分:3)

在Swift版本中:

    let imageData = UIImagePNGRepresentation(image!)!
    let imageFromData = UIImage(data: imageData)

答案 3 :(得分:0)

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

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

注意:数据是Swift 3类。使用数据而不是NSData   斯威夫特3

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

答案 4 :(得分:0)

从图像转换数据:

根据您的图像格式尝试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)