Swift Base64 Format - swift

On this website, for example when you drop in an image, it is turned into a proper base64 format: http://base64image.org/
In my Swift app, here is what I have to generate a base64 string from an image:
let image_Data = UIImagePNGRepresentation(default_image)
let base64String = image_Data!.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
This gives me something similar, but it has spaces and line breaks. How do I get an output just like the output from the website above?

let image_Data = UIImagePNGRepresentation(default_image)
let base64String = image_Data!.base64EncodedStringWithOptions([]) // Don't ask for line breaks
If you remove the request for line breaks, does it match what you're expecting?

Related

How to append a string to a URL in Swift?

Given a name of a file like this:
let fileName = "example.jpg"
I am grabbing the URL of this directory using a helper function, which returns the URL to me:
let imagesURL = getURL()
How can I append the string fileName to the imagesURL such that it returns something like this:
/path/to/images/dir/example.jpg
How do I get it in both String and URL format?
This gives you a new URL with the fileName appended:
let appended = imagesURL.appendingPathComponent(fileName)
And this converts it back to a string:
let strVersion = appended.absoluteString //full URL
let strVersion2 = appended.path //path only
url.appendingPathComponent(fileName)
has been deprecated, use
url.appending(path: filename) instead.

Using String addingPercentEncoding to encode a string as query param

I have an input string "+20" and I am trying to pass that as query parameter in url.
So I am trying to encode the myInputString by doing
let s1 = myInputString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
But in the debugger, the string s1 still shows as '+20' instead of '%2B20'
Is there something I did wrong?
As already mentioned by matt + is a legal URL character. If you really need to encode it you would need to create your own custom urlQueryAllowed and subtract the plus sign from it:
extension CharacterSet {
static let allowedCharacters = urlQueryAllowed.subtracting(.init(charactersIn: "+"))
}
let myInputString = "+20"
let s1 = myInputString.addingPercentEncoding(withAllowedCharacters: .allowedCharacters) // "%2B20"

how to convert fileurl(swift) to encoded utf8 hex?

i just want to convert fileURLWithPath to utf8 hex format as like bellow swift code .
import Cocoa
// this swift file name is aaa.swift
let arguments = CommandLine.arguments
let str = "\(arguments)"
let url = URL(fileURLWithPath: str)
print(url)
and i send bash path variable to aaa.swift file . like below
path="/Volumes/aaa/ccc.mov"
swift aaa.swift $path
then print out on terminal like below
%5B%22./aaa.swift%22,%20%22/Volumes/aaa/ccc.mov%22%5D -- file:///Volumes/aaa/
i want to get "/Volumes/aaa/ccc.mov" this part ..
how to do this ??

Urlencode cyrillic characters in Swift

I need to convert a cyrillic string to its urlencoded version using Windows-1251 encoding. For the following example string:
Моцарт
The correct result should be:
%CC%EE%F6%E0%F0%F2
I tried addingPercentEncoding(withAllowedCharacters:) but it doesn't work.
How to achieve the desired result in Swift?
NSString has a addingPercentEscapes(using:) method which allows to specify an arbitrary
encoding:
let text = "Моцарт"
if let encoded = (text as NSString).addingPercentEscapes(using: String.Encoding.windowsCP1251.rawValue) {
print(encoded)
// %CC%EE%F6%E0%F0%F2
}
However, this is deprecated as of iOS 9/macOS 10.11. It causes compiler warnings and may not be available in newer OS versions.
What you can do instead is to convert the string do Data with
the desired encoding,
and then convert each byte to the corresponding %NN sequence (using the approach from
How to convert Data to hex string in swift):
let text = "Моцарт"
if let data = text.data(using: .windowsCP1251) {
let encoded = data.map { String(format: "%%%02hhX", $0) }.joined()
print(encoded)
// %CC%EE%F6%E0%F0%F2
}

Base64Encoding result string with "/" in Swift

I want to convert a UIImage to a string representation. I am using the following code:
let imageData = UIImagePNGRepresentation(resizedImage)
if let imageBase64 = imageData?.base64EncodedDataWithOptions(NSDataBase64EncodingOptions (rawValue: 0)) {
let strBase64:String = imageBase64.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
}
The resulting string looks something like:
aVZCT1J3MEtHZ29BQUFBTlNVaEVVZ0FBQWZRQUFBRk5DQUlBQUFCNWNRcGdBQUFBQVhOU1IwSUFyczRjNlFBQUFCeHBSRTlVQUFBQUFnQUFBQUFBQUFDbkFBQUFLQUFBQUtjQUFBQ21BQUg4T0dIYnkwY0FBRUFBU1VSQlZIZ0J0TDMxbHh4SHR1L3JQK25kYzk5ZDU3NHpaK0I0NXN5Y0FkUFlIbnRNR3RzeWcyekpZdWdXWTNjTEd0UnFadTVxWm1abVptWUd0Vmp2RzdFemQwVmxabFZYeXpOZTN4VnJaMlJXU2ZybFUxOS9ZMGZrUzdQTEQrZFdIczZ2UGxwWWZieTQ5bVJwL2NueStxT1ZqY2ZRNnVZVGFHM3JxZFRqOWUwbkxyUzI5ZGdnRncrdmJ6LzdtZHE0LzN4bDQxRjljOXZBK0p5enI5TC81dlQzZHpyU3Y1SCtzU3Y0OTI0OFc5dDRqa0xxMmNxbVhjc2J6d3hhV245cTBOemEwL24xWnhnTm1sMTk0a3d6cTQ5VlRhODhZVTB0UDJ
But the format I am looking for should contain "/", like the following (a random image I found):
/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxMTEhUSEhMWFRUWGBcZGRgXFxcVFRgYFxUWFhcVFxgYHSggGBolGxcXITEhJSkrLi4uFx8zODMtNygtLisBCgoKDg0OGhAQGisdHx0tLS0tKy0tLS0tLSstLS0tLS0tLS0tKy0tLS0tLSstLS0tLSs3Ky0tKysrLSstKysrK//AABEIAMwAzAMBIgACEQEDEQH/xAAcAAABBAMBAAAAAAAAAAAAAAAGAgMEBQABBwj/xABCEAABAwIDBQUGAwYFBAMBAAABAgMRAAQSITEFBkFRYRMicYGRBzJCobHRFCPBUmKCk+HwM0NTctIVkqLxRGOyJP/
I don't know how to get the second format (containing the "/").
You're encoding it twice. Just do
let strBase64 = imageData?.base64EncodedStringWithOptions([])