0

There are a few questions similar to this, but most relate to Objective C and I was not able to find a solution.

How can I convert a String to a UIImage in Swift 4? This is what I have so far:

func convertStringToUIImage (image: String) -> UIImage {

    let imageData = image.data(using: String.Encoding.utf8)
    print ("imageData: ", imageData!)

    let base64Image = imageData!.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
    print ("base64Image: ", base64Image)

    let base64ImageAsData =  Data(base64Encoded: base64Image as String, options: NSData.Base64DecodingOptions())
    print ("base64ImageAsData: ", base64ImageAsData!)

    let dataAsUIImage = UIImage(data: base64ImageAsData!)
    print ("dataAsUIImage: ", dataAsUIImage)

    if dataAsUIImage != nil {

        print (dataAsUIImage)
        return dataAsUIImage!

    } else {

        print (dataAsUIImage)
        return #imageLiteral(resourceName: "DefaultImage.jpg")

    }

}

And that returns the following sequence of events in my console. The first two are from earlier print statements, but that 'lastValue' string is the string I want to convert:

Test Guy 
lastValue =  105827994889760
imageData:  15 bytes
base64Image:  MTA1ODI3OTk0ODg5NzYw
base64ImageAsData:  15 bytes
dataAsUIImage:  nil
nil

The image parameter passed into the function is the result of another function I wrote - convertUIImageToString(image: UIImage). It is stored in a String dictionary:

func convertUIImageToString (image: UIImage) -> String {

    var imageAsData: Data = UIImagePNGRepresentation(image)!
    var imageAsInt: Int = 0 // initialize

    let data = NSData(bytes: &imageAsData, length: MemoryLayout<Int>.size)
    data.getBytes(&imageAsInt, length: MemoryLayout<Int>.size)

    let imageAsString: String = String (imageAsInt)

    return imageAsString

}

Xcode tells me that UIImage(data: dataSource) will return "An initialized UIImage object, or nil if the method could not initialize the image from the specified data." It is returning nil, so why can't it read the data I'm specifying?

Edit: Added my convertUIImageToString function to clarify where the image parameter in convertStringtoUIImage comes from.

rtoken
  • 303
  • 6
  • 14
  • 1
    What does `image` represent? Is it a base64 representation of an image? Where did it come from? How long is `image`? If the `lastValue` output shown in your question is actually the value of `image` then there is no way it represents an image. – rmaddy Jan 13 '18 at 19:42
  • Are you trying to convert UIImage to string and back to UIImage? – Leo Dabus Jan 13 '18 at 19:55
  • See my edit and code: the image parameter is a String value that comes from a different function I wrote that converts a UIImage to a String. That String is then saved in a dictionary. I later need to convert the saved String back to a UIImage. – rtoken Jan 13 '18 at 19:55
  • @LeoDabus - Yes – rtoken Jan 13 '18 at 19:55
  • Your method to convert an image to a string isn't even remotely correct. There are many examples of converting a `UIImage` to a base64 encoded string (and back) in Swift. Please do a little searching. – rmaddy Jan 13 '18 at 19:56
  • That explains the issue then - though I have done a little searching; as that is where I found the basics of the function I'm using to convert a UIImage to a String. Does my function to convert a String to UIImage look correct? – rtoken Jan 13 '18 at 19:57
  • 1
    No, both your methods are all wrong. See https://stackoverflow.com/questions/39173048/faster-uiimage-base64-conversion Rob's answer is what you want. Simple and clean. – rmaddy Jan 13 '18 at 19:57
  • Upvoted, thanks for the suggestion. Didn't find that when searching around on this site. – rtoken Jan 13 '18 at 20:13

0 Answers0