NSData to UIImage

前端 未结 5 2128
逝去的感伤
逝去的感伤 2020-11-29 08:40

I\'m trying to save a UIIMage and then retrieve it and display it. I\'ve been successful by saving an image in the bundle, retrieving it from there and displaying. My proble

相关标签:
5条回答
  • 2020-11-29 08:57

    Try this code. This worked for me.

    Saving the data.

    create path to save the image.

    let libraryDirectory = NSSearchPathForDirectoriesInDomains(.libraryDirectory,
                                                                   .userDomainMask,
                                                                   true)[0]
    let libraryURL = URL(fileURLWithPath: libraryDirectory, isDirectory: true)
    let fileURL = libraryURL.appendingPathComponent("image.data")
    

    convert the image to a Data object and save it to the file.

    let data = UIImageJPEGRepresentation(myImage, 1.0)
    try? data?.write(to: fileURL)
    

    retrieving the saved image

    let newImage = UIImage(contentsOfFile: fileURL.relativePath)
    

    Create an imageview and load it with the retrieved image.

    let imageView = UIImageView(image: newImage)
    self.view.addSubview(imageView)
    
    0 讨论(0)
  • 2020-11-29 08:57

    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-11-29 08:58

    You should be able to use class methods of UIImage

    [UIImage imageWithContentsOfFile:topPathToFile]; 
    

    OR

    [UIImage imageWithData:data];
    

    Did that not work?

    Hope this helps!

    0 讨论(0)
  • 2020-11-29 09:13

    Check this out: http://www.nixwire.com/getting-uiimage-to-work-with-nscoding-encodewithcoder/.

    It has exactly what I think the solution to your problem is. You can't just make an image out of NSData, even though that's what common sense suggests. You have to use UIImagePNGRepresentation. Let me know if this helps.

    0 讨论(0)
  • 2020-11-29 09:14

    Just in case this helps someone, from iOS6 we have the imageWithData:scale method. To get an UIImage with the right scale from an NSData object, use that method, declaring the scale you use to store the original image. For example:

    CGFloat screenScale = [[UIScreen mainScreen] scale];
    UIImage *image = [UIImage imageWithData:myimage scale:screenScale];
    

    Here, myimage is the NSData object where you stored the original image. In the example, I used the scale of the screen. If you use another scale for the original image, use that value instead.

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