Faster UIImage - Base64 conversion
<p>I am doing work where I have to encode and decode between <code>UIImage</code> and Base 64 string). This works great with smaller images, it takes less than 1 second to do the conversion forward and backward, but when I apply it to larger images it takes a long time, almost a minute.</p>

<p>Is there any other way to encode and decode <code>UIImage</code> objects to string to save them in SQLite database? Or if there is no other way to improve this, is there something else I can do to get this job done and I could get rid of the problem?</p>

<p>These are the extension methods I use to do the work:</p>

<pre><code>extension String {
    var toUIImage: UIImage? {
        var img = self
        img = String(img.characters.map({ $0 == "\r" ? " " : $0 }))
        img = String(img.characters.map({ $0 == "\n" ? " " : $0 }))
        img = String(img.characters.map({ $0 == "\r\n" ? " " : $0 }))
        let dataDecoded:NSData? = NSData(base64EncodedString: img, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)
        return UIImage(data: dataDecoded!)
    }
}

extension UIImage {
    var toString: String {
        let data = UIImagePNGRepresentation(self)
        let encoded =  data!.base64EncodedDataWithOptions(.Encoding64CharacterLineLength)
        var cadena = String(data: encoded, encoding: NSUTF8StringEncoding)!

        cadena = String(cadena.characters.map({ $0 == "\r" ? " " : $0 }))
        cadena = String(cadena.characters.map({ $0 == "\n" ? " " : $0 }))
        cadena = String(cadena.characters.map({ $0 == "\r\n" ? " " : $0 }))

        return cadena
    }
}
</code></pre>