I am getting base64 string with extension and I want to convert base64 string to GIF and display it in ImageView. I am using iOSDevCenters+GIF.swift file. I am getting NSData from string but when data converted in image, its giving nil.Below is my code:
let imageData = profileImageString.data(using: .utf8)
self.thumbnailMedia.image = UIImage.gifImageWithData(imageData!)
Does anybody have any ideas on how to do this?
If you are starting from a base64 string, you should decode it as a base64 string not UTF8.
if let data = Data(base64Encoded: imageDataString) {
let image = UIImage(data: data)
}
This snippet simply takes the encode image string, decode into a Data object and create an image from the data.
If you are working a lot using base64 string I strongly suggest you to extend the String structure functionalities.
extension String {
//: ### Base64 encoding a string
func base64Encoded() -> String? {
if let data = self.data(using: .utf8) {
return data.base64EncodedString()
}
return nil
}
//: ### Base64 decoding a string
func base64Decoded() -> String? {
if let data = Data(base64Encoded: self) {
return String(data: data, encoding: .utf8)
}
return nil
}
}
This snippet was taken from Github, credits to Stringer.
Also another way is use the extension created by Leo Dabus that is compliant with Swift convention:
extension String {
var data: Data { return Data(utf8) }
var base64Encoded: Data { return data.base64EncodedData() }
var base64Decoded: Data? { return Data(base64Encoded: self) }
}
extension Data {
var string: String? { return String(data: self, encoding: .utf8) }
}
Related
I have used the following code to encode/decode the string which has emojis.
extension String {
func encodeEmoji() -> String {
let data = self.data(using: .nonLossyASCII, allowLossyConversion: true)!
return String(data: data, encoding: .utf8)!
}
func decodeEmoji() -> String? {
let data = self.data(using: .utf8)!
return String(data: data, encoding: .nonLossyASCII)
}
}
I have called this function like this below. Converted the response in the 'User' model.
let user = User() // Loaded API's response in this model
let textWithEmoji = user.aboutMe.decodeEmoji() //Here, I am getting the string as the same as before decoding
lblAboutMe.text = textWithEmoji
Following is the encoded string which is not decoding:
"I love too...\n\u2705 Laugh \uD83D\uDE02\n\u2705 Read novels \uD83D\uDCDA\n\u2705 Watch movies \uD83C\uDFAC\n\u2705 Go for bike rides \uD83D\uDEB5\uD83C\uDFFD\u200D\u2640\uFE0F\n\u2705 Go for long walks \uD83D\uDEB6\uD83C\uDFFD\u200D\u2640\uFE0F\n\u2705 Cook \uD83D\uDC69\uD83C\uDFFD\u200D\uD83C\uDF73\n\u2705 Travel \uD83C\uDDEA\uD83C\uDDFA\uD83C\uDDEE\uD83C\uDDF3\uD83C\uDDEC\uD83C\uDDE7\n\u2705 Eat \uD83C\uDF2E\uD83C\uDF5F\uD83C\uDF73\n\u2705 Play board games \u265F\n\u2705 Go to the theatre \uD83C\uDFAD\nMy favourite season is autumn \uD83C\uDF42, i love superhero movies \uD83E\uDDB8\u200D\u2642\uFE0F and Christmas is the most wonderful time of the year! \uD83C\uDF84"
Here is the original text image:
The string you are using is invalid ("I love too...\n\u2705 Laugh \uD83D\uDE02\n\u2705 Read novels \uD83D\uDCDA\n\u2705 Watch movies \uD83C\uDFAC\n\u2705")
It should be in valid String literal
"\\uD83D\\uDCDA\\u2705"
You have a string non-BMP characters in form of JSON String. And your decodeEmoji cannot convert them into valid characters.
So we need to forcefully convert such strings.
extension String {
var jsonStringRedecoded: String? {
let data = ("\""+self+"\"").data(using: .utf8)!
let result = try! JSONSerialization.jsonObject(with: data, options: .allowFragments) as! String
return result
}
}
After that you need to decode emoji from above string using below function.
extension String {
var decodeEmoji: String? {
let data = self.data(using: String.Encoding.utf8,allowLossyConversion: false);
let decodedStr = NSString(data: data!, encoding: String.Encoding.nonLossyASCII.rawValue)
if decodedStr != nil{
return decodedStr as String?
}
return self
}
}
Usually JSON decoder can decode these type of characters into emoji
May be there is chances of invalid JSON
First need to verify these things that json is valid or not before using.
USAGE:
let jsonDecodedString = "Your string".jsonStringRedecoded
let decodedEmojiText = jsonDecodedString?.decodeEmoji
debugPrint("\(decodedEmojiText)")
Twitter Api Response:
"retweet_count" = 0;
retweeted = 0;
source = "Twitter for Android";
text = "ALTIN alm\U0131\U015f ba\U015f\U0131n\U0131 gidiyor... bakal\U0131m t\U00fcrk liras\U0131 daha ne kadar de\U011fersizle\U015fecek #Turkiye #BorsaAltin\U2026 https://twitter.com/i/web/status/1216306036602277889";
truncated = 1;
My code:
let request = client.urlRequest(withMethod: "GET", urlString: statusesShowEndpoint, parameters: params, error: &clientError)
client.sendTwitterRequest(request) { (response, data, connectionError) -> Void in
if connectionError != nil {
print("Error: \(connectionError)")
}
do {
let json = try JSONSerialization.jsonObject(with: data!, options: [])
print("json: \(json)")
} catch let jsonError as NSError {
print("json error: \(jsonError.localizedDescription)")
}
}
}
how can I convert Unicode to string ? i have been don't use model.
I hope this will help.
I made a playground to test this issue. Swift will complain that the String contains incorrect escape characters.
This is a problem and the code will not compile:
Swift expect the Unicode to be formatted with ex '\u0131' or '\u{0131} but you receive the twitter API with '\U0131'
You need to "sanitise" the input first otherwise it will not work! Also when I tested the below I could not save the input in a string with the incorrect escaping. The compiler checks that the strings are correct before doing any operations on them.
I used a trick. Before saving the input from the file I split into an array of characters with map, of these characters I check with filter which one is an escaping backlash remove it and join the characters again to form a string.
Sorry but I did not find any other way, just having '\U' in my input would get Swift yelling at me.
What remains in the input string is "ALTIN almU0131U015f ..etc"
Now I need to replace those 'U0131' with '\u0131' and for this I use Regex:
And this is the final output of my String test as property of my struct after the conversion.
I apologise if my code is a bit messy but it was not easy to get past the string validation in Swift!
The below is the playground code in detail.
What I did is to create a json file with your input as a test:
Then create a struct reflecting the JSON properties, in this case only one: "test"
public struct Test: Codable {
var test = ""
}
// this is the initialisation of the empty struct to be filled from json
var unicodetest: Test = Test()
func parse(json: Data) {
let decoder = JSONDecoder()
if let testmodel = try? decoder.decode(Test.self, from: json) {
unicodetest = testmodel
print(unicodetest.test)
}
}
// Here I get my Json and parse with the above function
do {
guard let fileUrl = Bundle.main.url(forResource: "test", withExtension: "json") else { fatalError() }
let input = try String(contentsOf: fileUrl, encoding: String.Encoding.utf8).map {String($0)}.filter {$0 != "\\"}.joined(separator: "")
if let sanitisedData = input.replacingOccurrences(of: "U(.*?)", with: "\\\\u$1", options: .regularExpression).data(using: .utf8){
parse(json: sanitisedData)
}
} catch {
// if something did not work out
print(error)
}
You can use \u{Unicode}:
print("Ain\u{2019}t i am a smart boy")
/* Prints "Ain’t i am a smart boy"
You can Use this Extension as well
extension String {
var unescapingUnicodeCharacters: String {
let mutableString = NSMutableString(string: self)
CFStringTransform(mutableString, nil, "Any-Hex/Java" as NSString, true)
return mutableString as String
}
}
let input = "ALTIN alm\\u0131\\u015f ba\\u015f\\u0131n\\u0131 gidiyor... bakal\\u0131m t\\u00fcrk liras\\u0131 daha ne kadar de\\u011fersizle\\u015fecek #Turkiye #BorsaAltin\\u2026 https://twitter.com/i/web/status/1216306036602277889"
print("result: \(input.unescapingUnicodeCharacters)")
//ALTIN almış başını gidiyor... bakalım türk lirası daha ne kadar değersizleşecek #Turkiye #BorsaAltin… https://twitter.com/i/web/status/1216306036602277889
Im trying to convert a base64 string to an UIImage
First. I decode the String as shown below
let decodedData = NSData(base64Encoded: decodeIMG, options: NSData.Base64DecodingOptions(rawValue: 0))
Then I try to convert the decoded data to an UIImage like this:
let decodedIamge = UIImage(data: decodedData as Data)
But on that line I get the following error:
Cannot convert value of type 'NSData?' to type 'Data' in coercion
I already tried using another approach to convert it by using an extension that looks like this
extension String {
//: ### Base64 encoding a string
func base64Encoded() -> String? {
if let data = self.data(using: .utf8) {
return data.base64EncodedString()
}
return nil
}
//: ### Base64 decoding a string
func base64Decoded() -> String? {
if let data = Data(base64Encoded: self) {
return String(data: data, encoding: .utf8)
}
return nil
}
}
And get this error
Incorrect argument label in call (have 'base64Encoded:', expected
'map:')
i am new in IOS and i am making a project in which i receive Base64 data from web service. how to convert Base64 data into string and how to open a pdf view in swift and also check that is there any pdf owner application install or not in iPhone .and i want to know that how to convert NSDATA in string swift.Help me
example like this is Base64 data
JVBERi0xLjQKJcfsj6IKNSAwIG9iago8PC9MZW5ndGggNiAwIFIvRmlsdGVyIC9GbGF0ZURlY29kZT4+CnN0cmVhbQp4nLVcza8ct5GH1rK9ejIU24lkO5GtJ8vWm5E87eY3uXtbYLHAYi8JdItySrABAjhA8v8fUuwmu35kF2fmxbsWDMxjk8VisapYX+TfbudJ6ds5/6s//vj
Like this is nsdata
<25504446 2d312e34 0a25c7ec 8fa20a35 2030206f 626a0a3c 3c2f4c65 6e677468 20362030 20522f46 696c7465 72202f46 6c617465 4465636f 64653e3e 0a737472 65616d0a 789cb55c cdaf1cb7 9187d6b2 bd7a3214 db89643b 91ad27cb d69b913c
There are two parts to this question. First, converting the base 64 string into a Data/NSData. But you've done that already, so you don't need help there.
Second, converting that Data/NSData into a string. But, if you look at that file carefully, you'll see that data is a PDF file, not a text string. For example, if I save that as a file and look at it in a hex editor, I can clearly see it's a PDF:
You can't just convert that PDF binary data to a string. (In fact, that's why it was base64-encoded in the first place, because it was complex binary data.)
But you can, for example, use UIDocumentInteractionController to preview the PDF file that you saved to a file.
For example:
// convert base 64 string to data
let base64 = "JVBERi0xLjQKJcfsj6IKNSAwIG9iago8PC9MZW5ndGggNiAwIFIvRmlsdGVyIC9GbGF0ZURlY29kZT4+CnN0cmVhbQp4nLVcza8ct5GH1rK9ejIU24lkO5GtJ8vWm5E87eY3uXtbYLHAYi8JdItySrABAjhA8v8fUuwmu35kF2fmxbsWDMxjk8VisapYX+TfbudJ6ds5/6s/"
guard let data = Data(base64Encoded: base64) else {
print("unable to convert base 64 string to data")
return
}
// given the data was PDF, let's save it as such
let fileURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
.appendingPathComponent("test.pdf")
try! data.write(to: fileURL)
// if it was a string, you could convert the data to string, but this will fail
// because the data is a PDF, not a text string
//
// guard let string = String(data: data, encoding: .utf8) else {
// print("Unable to convert data into string")
// return
// }
// print(string)
// So, instead, let's use `UIDocumentInteractionController` to preview the PDF:
let controller = UIDocumentInteractionController(url: fileURL)
controller.delegate = self
controller.presentPreview(animated: true)
Where, the view controller conforms to UIDocumentInteractionControllerDelegate:
extension ViewController: UIDocumentInteractionControllerDelegate {
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
return self
// or, if this view controller is already in navigation controller, don't
// return `self`, like above, but instead return the navigation controller itself
//
// return navigationController!
}
}
From Base64 to Data to String.
let base64String = "dGhpcyBpcyBmb3IgdGVzdGluZw=="
if let data = Data(base64Encoded: base64String) {
if let string = String(data: data, encoding: .utf8) {
print(string)
}
}
From Base64 to NSData to String.
let data = NSData(base64Encoded: base64String, options: .ignoreUnknownCharacters)
var string = String(data: data, encoding: .utf8)
}
Swift 3.0
A handy extension of string. Hope will help you.
extension String {
func fromBase64() -> String? {
guard let data = Data(base64Encoded: self) else {
return nil
}
return String(data: data, encoding: .utf8)
}
func toBase64() -> String {
return Data(self.utf8).base64EncodedString()
}
}
I have the following extension:
extension String {
func fromBase64() -> String? {
guard let data = NSData(base64EncodedString: self, options: NSDataBase64DecodingOptions(rawValue: 0)) else {
return nil
}
return String(data: data, encoding: NSUTF8StringEncoding)!
}
func toBase64() -> String? {
guard let data = self.dataUsingEncoding(NSUTF8StringEncoding) else {
return nil
}
return data.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
}
}
And I have a base 64 encoded string: R79gQDNTt/0+cjU7pduqfA==
func doStuff(sample : String){
let sampleBase64Decoded = sample.fromBase64()
}
When calling this function, (doStuff("R79gQDNTt/0+cjU7pduqfA==")) I get a runtime error referring to finding nil while unwrapping the optional:
return String(data: data, encoding: NSUTF8StringEncoding)!
What reasons would there be for Swift not being able to convert the base64 data to a string?