convert UIImage to NSData and back to UIImage

前端 未结 5 1468
难免孤独
难免孤独 2020-12-31 15:13

I have to populate table cell from database and images are on device. I\'ve to store that image in imageDataObject, how to convert that UIImage to

相关标签:
5条回答
  • 2020-12-31 15:28

    in Swift version:

        let imageData = UIImagePNGRepresentation(image!)!
        let imageFromData = UIImage(data: imageData)
    
    0 讨论(0)
  • 2020-12-31 15:31

    Use if-let block with Data to prevent app crash & safe execution of code, as function UIImagePNGRepresentation returns an optional value.

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

    By using extension:

    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...
         }
    }
    
    0 讨论(0)
  • 2020-12-31 15:32

    Convert Data from Image:

    Try JPEG or PNG representation depending on your image format:

    UIImageJPEGRepresentation

    Returns the data for the specified image in JPEG format.

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

    UIImagePNGRepresentation

    Returns the data for the specified image in PNG format

    NSData * UIImagePNGRepresentation (
       UIImage *image
    );
    

    Example:

    extension UIImage {
    
        var pngRepresentationData: Data? {
            return UIImagePNGRepresentation(img)
        }
    
        var jpegRepresentationData: Data? {
            return UIImageJPEGRepresentation(self, 1.0)
        }
    }
    

    Use:

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

    Convert Image from Data:

    let image = UIImage(data: data)
    
    0 讨论(0)
  • 2020-12-31 15:36

    Try With this:

    Use this for convert UIImage to NSData.

      NSData *imagedata = UIImagePNGRepresentation(thumbnail);
    

    Try this for image data convert back to UIImage

      UIImage *image= [UIImage imageWithData:imagedata];
    
    0 讨论(0)
  • 2020-12-31 15:40

    To convert UIImage to NSData, use either UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality) or UIImagePNGRepresentation(UIImage *image)

    To convert NSData to UIImage, use [UIImage imageWithData:imageData]

    So your example could look like this:

    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];
    

    References: 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

    0 讨论(0)
提交回复
热议问题