How to convert a Base64 string to NSData?

I have an iOS application (written in Swift) that retrieves data from a wcf service in JSON format. One of the data is an image saved as base64string . However, I was not able to convert base64string to NSData strong>.

My main goal is to completely convert base64string to blob so that I can save this to the database. On the other hand, if you know at least part of it, for example, from base64string to NSData strong>.

The following code will give you an idea of ​​my table

let ItemsDB = Table("Items")
let idDB = Expression<String>("ID")
let nameDB = Expression<String>("Name")
let catDB = Expression<String>("Category")
let uomDB = Expression<String>("UOM")
let priceDB = Expression<Double>("Price")
let imageDB = Expression<Blob>("Image")
let actDB = Expression<Bool>("Active")
+6
3

Base64 NSData​​p >

 let nsd: NSData = NSData(base64EncodedString: Image, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)!

Blob

nsd.datatypeValue
+4

, , Objective-c. , NSData base64

NSData(base64EncodedString: String, options: NSDataBase64DecodingOptions)

+1

It works:

Swift 3, 4, and 5:

var data = Data(base64Encoded: recording_base64, options: .ignoreUnknownCharacters)

Swift 2:

var data = NSData(base64EncodedString: recording_base64, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)
0
source

Source: https://habr.com/ru/post/1628322/


All Articles