-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageIntoStringVCViewController.swift
65 lines (31 loc) · 1.19 KB
/
ImageIntoStringVCViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//
// ViewController.swift
// SampleApp
//
// Created by ITC Infotech on 05/07/17.
// Copyright © 2018 Rahul Patil. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
guard let customImage = UIImage(named: "icon") else {
return
}
// get NSData object from UIImage.
let imageData = UIImagePNGRepresentation(customImage) as NSData?
// convert image into string with base64Encoding
guard let imageInString = imageData?.base64EncodedString(options: Data.Base64EncodingOptions.lineLength64Characters) else {
return
}
print("Image in String : - \(imageInString)")
// convert string into image
guard let imageRevertData = Data(base64Encoded: imageInString, options: Data.Base64DecodingOptions.ignoreUnknownCharacters) else {
return
}
guard let originalImageBack = UIImage(data: imageRevertData) else {
return
}
print("Revert back original image : - \(originalImageBack)")
}
}