I have a encoded string from server which is a image.
Now I want to decode that string to anything(UIImage) you can and set to a UIImageView
Thanks in advance
I'm using the following extension for that:
extension UIImage: {
convenience init?(base64 str: String) {
guard let url = URL(string: str),
let data = try? Foundation.Data(contentsOf: url),
UIImage(data: data) != nil else { return nil }
self.init(data: data)!
}
}
Then you can do:
let image = UIImage(base64: someString)
Note, that for this to work, your base64 string must be formatted as follows: data:image/png;base64,... (or replacing png with jpeg).
If your base64 string does not have this prefix, you can do this:
extension UIImage: {
convenience init?(base64 str: String) {
guard let data = try? Foundation.Data(base64Encoded: str),
UIImage(data: data) != nil else { return nil }
self.init(data: data)!
}
}
Extra Credit
You can reverse the whole thing and convert an image to base64 string:
public enum ImageFormat {
case png
case jpeg
}
public func base64(format: ImageFormat, includingPrefix: Bool = true) -> String? {
var imageData: Foundation.Data?
switch format {
case .png: imageData = self.pngData
case .jpeg: imageData = self.jpegData
}
guard let data = imageData else { return nil }
return "data:image/\((format));base64," + data.base64EncodedString()
}
You can do the following:
let string: String = "YourBase64EncodedImageAsAString"
if let imageData = Data(base64Encoded: string), let image = UIImage(data: imageData) {
//use image
}
func base64ToImage(base64String: String?) -> UIImage{
if (base64String?.isEmpty)! {
return #imageLiteral(resourceName: "no_image_found")
}else {
// Separation part is optional, depends on your Base64String !
let tempImage = base64String?.components(separatedBy: ",")
let dataDecoded : Data = Data(base64Encoded: tempImage![1], options: .ignoreUnknownCharacters)!
let decodedimage = UIImage(data: dataDecoded)
return decodedimage!
}
}
You will pass args to your base64 data.
Related
I'm making an Application with a java backend and a Swift front-end. Using a REST Api to move data. I wish to encrypt the data with AES 128 CBC. The encrypting method is working, but the decrypting method is not.
First off all, this is the Swift code for de AES encrypting and decrypting:
import Foundation
import CommonCrypto
struct AES {
private let key: Data
private let iv: Data
init?() {
let ivProduct: String = "dkghepfowntislqn"
let keyProduct: String = "2949382094230487"
guard keyProduct.count == kCCKeySizeAES128 || keyProduct.count == kCCKeySizeAES256, let keyData = keyProduct.data(using: .utf8) else {
debugPrint("Error: Failed to set a key.")
return nil
}
guard ivProduct.count == kCCBlockSizeAES128, let ivData = ivProduct.data(using: .utf8) else {
debugPrint("Error: Failed to set an initial vector.")
return nil
}
self.key = keyData
self.iv = ivData
}
func encrypt(string: String) -> Data? {
return crypt(data: string.data(using: .utf8), option: CCOperation(kCCEncrypt))
}
func decrypt(data: Data?) -> String? {
guard let decryptedData = crypt(data: data, option: CCOperation(kCCDecrypt)) else { return nil }
return String(bytes: decryptedData, encoding: .utf8)
}
func crypt(data: Data?, option: CCOperation) -> Data? {
guard let data = data else { return nil }
let cryptLength = data.count + kCCBlockSizeAES128
var cryptData = Data(count: cryptLength)
let keyLength = key.count
let options = CCOptions(kCCOptionPKCS7Padding)
var bytesLength = Int(0)
let status = cryptData.withUnsafeMutableBytes { cryptBytes in
data.withUnsafeBytes { dataBytes in
iv.withUnsafeBytes { ivBytes in
key.withUnsafeBytes { keyBytes in
CCCrypt(option, CCAlgorithm(kCCAlgorithmAES), options, keyBytes.baseAddress, keyLength, ivBytes.baseAddress, dataBytes.baseAddress, data.count, cryptBytes.baseAddress, cryptLength, &bytesLength)
}
}
}
}
guard UInt32(status) == UInt32(kCCSuccess) else {
debugPrint("Error: Failed to crypt data. Status \(status)")
return nil
}
cryptData.removeSubrange(bytesLength..<cryptData.count)
return cryptData
}
}
The data is gathered from the REST API like so:
func getTestAllPayments(_ completion: #escaping ([Payment]) -> ()) {
let aes128 = AES()
if let url = URL(string: "\(localhostUrl)/payment") {
URLSession.shared.dataTask(with: url) { data, response, error in
if let data = data {
do {
let res = try JSONDecoder().decode([Payment].self, from: (data))
print(res.self)
completion(res)
return
} catch let error {
print(error)
}
}
}.resume()
}
}
Now for the problem. I've ran a couple of test:
first check if the encrypt and decrypt methods work together:
let aes128 = AES()
let dataEncrypt = aes128?.encrypt(string:"Hello") //Will be :lG7Bqk0nwx732eOQLAzhqQ==
let dataDecrypt = aes128?.decrypt(data:dataEncrypt) //Will be: "Hello"
print(dataDecrypt) --> //output = "Hello"
First test works like a charm. For the second test:
let aes128 = AES()
if let url = URL(string: "\(localhostUrl)/payment") {
URLSession.shared.dataTask(with: url) { data, response, error in
if let data = data {
print(String(data: data, encoding: .utf8)) //Output = lG7Bqk0nwx732eOQLAzhqQ==
let dataDecrypt = aes128?.decrypt(data: data)
print(dataDecrypt) --> //output = nil
This is where it goes wrong. When fetching the data with the exact same encoding string, it'll always return nil. Has it something to do with the data format that URLSession returns?
I am trying to upload image in base64string format. Here i am uploading images with some parameters using alamofire and swift. But my image is not uploading to the server. Please check my code and let me know where i am doing wrong. My code is:
func postRegistrationPlayerProfile(urlExtension: String, callback: #escaping(Bool,String?) -> Void,parameters:[String:Any]) -> Void {
let fullURL = URL(string: urlExtension)!
let manager = Alamofire.SessionManager.default
manager.session.configuration.timeoutIntervalForRequest = TimeInterval(Common.sharedInstance().REQUEST_TIME_OUT)
manager.upload(multipartFormData: { (multipartFormData) in
for (key, value) in parameters {
multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)
}
}, usingThreshold: UInt64.init(), to: fullURL, method: .post) { (result) in
switch result {
case .success(let upload, _, _):
upload.responseJSON { (response : DataResponse<Any>) in
let responseDataString = Common.sharedInstance().getJSONStringFromData(data: response.data! as NSData)
print(responseDataString)
if let result = response.result.value {
let dictionary = result as! NSDictionary
let status = dictionary["status"] as! String
let message = dictionary["message"] as? String
if status == "success"{
callback(true,message)
return
}
else{
callback(false,message)
return
}
}
else{
callback(false,Common.sharedInstance().FAILED_BANNERIMG_UPLOAD)
return
}
}
case .failure(let encodingError):
print("encodingError: \(encodingError)")
callback(false,Common.sharedInstance().FAILED_BANNERIMG_UPLOAD)
}
}
}
my parameters are:
paramters.updateValue(phone!, forKey: "phone")
paramters.updateValue(state!, forKey: "state")
paramters.updateValue(city!, forKey: "city")
paramters.updateValue(zip!, forKey: "postcode")
paramters.updateValue(travel, forKey: "travel_distance")
paramters.updateValue(base64ImageString!, forKey: "profile_image")
paramters.updateValue(country!, forKey: "country")
I am converting my image to base64 string like:
if let selectedImage = chooseProfileImage.image{
if let data = selectedImage.jpegData(compressionQuality: 1){
base64ImageString = data.base64EncodedString(options: .lineLength64Characters)
}
}
Adding
url('data:image/jpeg;base64,)
to my Base64 converted string worked for me:
if let selectedImage = chooseProfileImage.image{
if let data = selectedImage.jpegData(compressionQuality: 1){
let imageString = selectedImage.jpegData(compressionQuality: 1.0)?.base64EncodedString() ?? ""
base64ImageString = "url('data:image/jpeg;base64,\(imageString)')"
profileData = data
}
}
I want to convert a bas64 string to an image. Here's the code I currently have.
let sData = success as! String
let str = String(utf8String: sData.cString(using: String.Encoding.utf8)!)
let decodedData = NSData(base64Encoded: str!, options: NSData.Base64DecodingOptions(rawValue: 0) )
let image = UIImage(data: decodedData! as Data)
if let decodedData = Data(base64Encoded: (yourBase64Dasta as? String)! , options: .ignoreUnknownCharacters) {
imgThumb = UIImage(data: decodedData)
}
this is currently working code in my swift 4 project so you can use it :)
Here's an extension you can use to easily initialize an image from a base43 string:
extension UIImage {
convenience init?(base64 str: String) {
guard let url = URL(string: str),
let data = try? Foundation.Data(contentsOf: url),
UIImage(data: data) != nil else { return nil }
self.init(data: data)!
}
}
Then you can do:
let image = UIImage(base64: "string from your server goes here")
I have UIImageView and I want to download images in cache if exist, I've used extension func.
I have this code but not working:
extension UIImageView {
func loadImageUsingCache (_ urlString : String) {
let imageCache = NSCache<AnyObject, AnyObject>()
if let cachedImage = imageCache.object(forKey: urlString as AnyObject) {
self.image = cachedImage as? UIImage
return
}
let url = URL(string: urlString)
URLSession.shared.dataTask(with: url!) { (data, response, error) in
if data != nil {
if let image = UIImage(data: data!) {
imageCache.setObject(image, forKey: urlString as AnyObject)
DispatchQueue.main.async(execute: {
self.image = image
})
}
}
}.resume()
}
}
You're creating the new NSCache object for each image and not retain it.
You should create object variable instead of local one. It won't work in extension in this case. Also you can try to use URLCache.shared instead.
// It's Perfect Solution //
var imageCache = String: UIImage
class CustomImageView: UIImageView {
var lastImgUrlUsedToLoadImage: String?
func loadImage(with urlString: String) {
// set image to nil
self.image = nil
// set lastImgUrlUsedToLoadImage
lastImgUrlUsedToLoadImage = urlString
// check if image exists in cache
if let cachedImage = imageCache[urlString] {
self.image = cachedImage
return
}
// url for image location
guard let url = URL(string: urlString) else { return }
// fetch contents of URL
URLSession.shared.dataTask(with: url) { (data, response, error) in
// handle error
if let error = error {
print("Failed to load image with error", error.localizedDescription)
}
if self.lastImgUrlUsedToLoadImage != url.absoluteString {
return
}
// image data
guard let imageData = data else { return }
// create image using image data
let photoImage = UIImage(data: imageData)
// set key and value for image cache
imageCache[url.absoluteString] = photoImage
// set image
DispatchQueue.main.async {
self.image = photoImage
}
}.resume()
}
}
I am receiving a base64String from webservice response in NSData, how to convert that base64String to String in swift?
//Code
var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as! NSDictionary // Response JSON from webservice
var base64String : String = ""
base64String = jsonResult["Base64String"] as! String // Retrieve base64String as string from json response
println("Base64String Alone: \(base64String)")
// Code to decode that base64String
let decodedData = NSData(base64EncodedString: base64String, options: NSDataBase64DecodingOptions(rawValue: 0))
println("Decoded: \(decodedData)")
let decodedString = NSString(data: decodedData!, encoding: NSUTF8StringEncoding)
println(decodedString) // Prints nil
Encode and decode of base64String works well for the files containing only text, if a file contains some table formats/images both encoding and decoding gives an invalid base64String. How to convert a file into base64String encode and decode whatever the contents of file ?
File formats are doc, docx, pdf, txt
Advance thanks for any help !
Try this:
let base64Encoded = "YW55IGNhcm5hbCBwbGVhc3VyZS4="
var decodedString = ""
if let decodedData = Data(base64Encoded: base64Encoded) {
decodedString = String(data: decodedData, encoding: .utf8)!
}
if !decodedString.isEmpty {
print(decodedString)
} else {
print("Oops, invalid input format!")
}
Make sure your base 64 encoded string is valid.
WARNING (edit)
In most base64 decoding implementations like Java, the padding-character is not needed, but Data(base64Encoded:) returns nil if it's missing.
Swift 5 solution; use String.fromBase64(_:) instead, after implementing like:
extension Data {
/// Same as ``Data(base64Encoded:)``, but adds padding automatically
/// (if missing, instead of returning `nil`).
public static func fromBase64(_ encoded: String) -> Data? {
// Prefixes padding-character(s) (if needed).
var encoded = encoded;
let remainder = encoded.count % 4
if remainder > 0 {
encoded = encoded.padding(
toLength: encoded.count + 4 - remainder,
withPad: "=", startingAt: 0);
}
// Finally, decode.
return Data(base64Encoded: encoded);
}
}
extension String {
public static func fromBase64(_ encoded: String) -> String? {
if let data = Data.fromBase64(encoded) {
return String(data: data, encoding: .utf8)
}
return nil;
}
}
As mentioned on editor's profile,
above edit's code allows Apache 2.0 license as well,
without attribution need.
Swift extension is handy.
extension String {
func base64Encoded() -> String? {
return data(using: .utf8)?.base64EncodedString()
}
func base64Decoded() -> String? {
guard let data = Data(base64Encoded: self) else { return nil }
return String(data: data, encoding: .utf8)
}
}
"heroes".base64Encoded() // It will return: aGVyb2Vz
"aGVyb2Vz".base64Decoded() // It will return: heroes
i've made an update to Ashok Kumar S answer to add filler character when string size is not divisible by 4, raising an exception and returning nil
extension String {
func base64Encoded() -> String? {
return data(using: .utf8)?.base64EncodedString()
}
func base64Decoded() -> String? {
var st = self;
if (self.count % 4 <= 2){
st += String(repeating: "=", count: (self.count % 4))
}
guard let data = Data(base64Encoded: st) else { return nil }
return String(data: data, encoding: .utf8)
}
You can encrypt/decrypt base64 strings using this extension:
public extension String {
var base64Decoded: String? {
guard let decodedData = Data(base64Encoded: self) else { return nil }
return String(data: decodedData, encoding: .utf8)
}
var base64Encoded: String? {
let plainData = data(using: .utf8)
return plainData?.base64EncodedString()
}
}
To Encode:
"Hello World!".base64Encoded
Result is an Optional string: "SGVsbG8gV29ybGQh"
To Decode:
"SGVsbG8gV29ybGQh".base64Decoded
Result is an Optional string: "Hello World!"
Source
The above answers are core, but i had an error like
fatal error, found nil while unwrapping an optional value
The solution is adding options
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, options: .ignoreUnknownCharacters) {
return String(data: data, encoding: .utf8)
}
return nil
}
}
and use it safely
var str = "HelloWorld"
if let base64Str = str.base64Encoded() {
print("Base64 encoded string: \"\(base64Str)\"")
if let trs = base64Str.base64Decoded() {
print("Base64 decoded string: \"\(trs)\"")
}
}