9

I'm kinda new to swift and i need some help with encoding some image, putting it in a JSON and after retrieving it, decoding it back to NSData and recreating the image in an UIImage view controller. I've found this post Convert Image to Base64 string in iOS + Swift but i get stuck with this part:

let decodedData = NSData(base64EncodedString: base64String, options: NSDataBase64DecodingOptions.fromRaw(0)!)

because the fromRaw method is not available anymore.

Thanks in advance

Later edit: I'm using swiftyJson to parse the array and i'm getting the image data like this:

var base64String = arrayJson[0]["photo"].stringValue
var imageString = base64String as NSString

and after that i'm trying to decode it like this:

let decodedData = NSData(base64EncodedString: imageString, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)

I've also tried with the rawValue instead of IgnoreUnknownCharacters. Both return nil. Also tried with the base64String instead of imageString. Same thing.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Adrian Stoicescu
  • 89
  • 1
  • 2
  • 4

4 Answers4

11

You can do the following instead to make a base64 encoded string to an UIImage:

 //base64 string to NSData
 let decodedData = NSData(base64EncodedString: base64String, options: NSDataBase64DecodingOptions(rawValue: 0))

//NSData to UIImage
var decodedIamge = UIImage(data: decodedData!)

NSDataBase64EncodingOptions.fromRaw(0)! now is changed to NSDataBase64DecodingOptions(rawValue: 0)

For more encode/decode details, you can visit this post: Convert between UIImage and Base64 string

Community
  • 1
  • 1
ztan
  • 6,861
  • 2
  • 24
  • 44
  • yes, the decodedData is nil. Basically, i'm putting in a mysql db through a JSON this value:let data = UIImageJPEGRepresentation(imageView.image, 50) let encodedImage = data.base64EncodedStringWithOptions(.allZeros) – Adrian Stoicescu Mar 02 '15 at 19:00
  • Have you tried to print the base64String or imageString? – ztan Mar 02 '15 at 19:06
  • yes.....the imageString starts like this:/9j/4AAQSkZJRgABAQEASABIAAD/4QCMRXhpZgAATU0AKgAAAAgABQESAAMAAAABAAEAAAEaAAUAAAABAAAASgEbAAUAAAABAAAAUgEoAAMAAAABAAIAAIdpAAQAAAABAAAAWgAAAAAAAAAAAAAAAQAAAAAAAAABAAOgAQA – Adrian Stoicescu Mar 02 '15 at 19:11
  • You might not encode your image correctly, you can try to covert your string to an image using this online convert: http://www.askapache.com/online-tools/base64-image-converter/ – ztan Mar 02 '15 at 19:18
  • i'm encoding it like this : let data = UIImageJPEGRepresentation(imageView.image, 50) let encodedImage = data.base64EncodedStringWithOptions(.allZeros) – Adrian Stoicescu Mar 02 '15 at 19:24
  • you can print your `encodedImage`(which is a string), and compare if it match the 'base64String` during your decode process. Also, you can encode your image in http://www.askapache.com/online-tools/base64-image-converter/, and use the string from converter, make sure you get rid of `data:image/png;base64` from the raw output. You can try this base 64 string from my gists file: https://gist.githubusercontent.com/ziyang0621/60c18dcb7c5bfa1eb762/raw/90c46541deab749f45d60110bfc17b9cd3b55575/gistfile1.txt – ztan Mar 02 '15 at 19:35
3

To encode an image:

let image = UIImage(...)
let quality = 1.0
let data: NSData = UIImageJPEGRepresentation(image, quality)!

To decode an image:

let decodedImage = UIImage(data: data)
kellanburket
  • 12,250
  • 3
  • 46
  • 73
1

The reason why these methods return nil for you could be that your base64string is an URL and Filename safe variant, meaning character 62 (0x3E) is replaced with a "-" (minus sign) and character 63 (0x3F) is replaced with a "_" (underscore) as noted here base64 alphabet

Try replacing character _ with / and character - with + in your string.

You could use the following code:

base64string = base64string.stringByReplacingOccurrencesOfString("-", withString: "+", options: NSStringCompareOptions.LiteralSearch, range: nil)
base64string = base64string.stringByReplacingOccurrencesOfString( "_" , withString: "/", options: NSStringCompareOptions.LiteralSearch, range: nil)

Also be aware of the correct length of the string. it has to be a multiple of 4 - take a look at this answer padded string.

Community
  • 1
  • 1
super123
  • 36
  • 5
0

NSData Class Reference : https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/index.html#//apple_ref/c/tdef/NSDataBase64DecodingOptions

NSDataBase64DecodingOptions :

struct NSDataBase64DecodingOptions : RawOptionSetType {
    init(_ rawValue: UInt)
    init(rawValue rawValue: UInt)
    static var IgnoreUnknownCharacters: NSDataBase64DecodingOptions { get }
}

Have you tried one of the followings :

  • let decodedData = NSData(base64EncodedString: base64String, options: NSDataBase64DecodingOptions(rawValue: 0)!)
  • let decodedData = NSData(base64EncodedString: base64String, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)
lchamp
  • 6,592
  • 2
  • 19
  • 27