更快的UIImage - Base64转换 [英] Faster UIImage - Base64 conversion

查看:136
本文介绍了更快的UIImage - Base64转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做的工作,我必须在 UIImage 和Base 64字符串之间进行编码和解码。这适用于较小的图像,向前和向后转换只需不到1秒,但当我将其应用于较大的图像时,需要很长时间,几乎一分钟。

I am doing work where I have to encode and decode between UIImage 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.

有没有其他方法可以将 UIImage 对象编码和解码为字符串以将它们保存在SQLite数据库中?或者,如果没有其他方法可以改善这一点,我还能做些什么来完成这项工作,我可以摆脱这个问题吗?

Is there any other way to encode and decode UIImage 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?

这些是我用来完成工作的扩展方法:

These are the extension methods I use to do the work:

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
    }
}


推荐答案

使用 String 扩展名,您只需使用 .IgnoreUnknownCharacters 即可必须完全替换 \ r \ n

With the String extension, you can just use .IgnoreUnknownCharacters and you don't have to do any replacement of \r and \n at all:

extension String {
    var imageFromBase64EncodedString: UIImage? {
        if let data = NSData(base64EncodedString: self, options: .IgnoreUnknownCharacters) {
            return UIImage(data: data)
        }
        return nil
    }
}

同样,在你的 UIImage 扩展名,不要使用 .Encoding64CharacterLineLength ,它会引入 \ r \ n ,然后才删除它们。如果您不使用该选项,则不会删除 \ r \ n

Likewise, in your UIImage extension, don't use .Encoding64CharacterLineLength, which introduces the \r and \n, only to then remove them. If you don't use that option, there is no \r and \n to remove:

extension UIImage {
    var base64EncodedString: String? {
        if let data = UIImagePNGRepresentation(self) {
            return data.base64EncodedStringWithOptions([])
        }
        return nil
    }
}






关于SQLite的两个观察结果:


Two observations regarding SQLite:


  1. 如果您要将图像存储在数据库中,只需将 NSData SQLite数据库存储为blob,完成它。这不仅消除了这个我怎么办 \ r \ n 字符,但它完全消失了完全消除了base64编码/解码过程。它还使您在数据库中存储的内容减小了25%。效率更高。

  1. If you're going to store image in database, just store the NSData SQLite database as blob and be done with it. That not only eliminates this "what do I do with \r and \n characters", but it completely eliminates the base64 encoding/decoding process entirely. It also makes the what you store in the database 25% smaller. It's a lot more efficient.

即使这样,SQLite在将大型对象存储在数据库中也是非常低效的。如果要获得最佳性能,请将映像存储在文件系统中,并仅在数据库中存储相对路径引用。我的经验法则是,如果我处理缩略图图像,SQLite blob就可以了,但对于任何更大的图像,请使用文件系统。

Even with that, SQLite is notoriously inefficient in storing large objects in database at all. If you want best performance, store images in the file system, and only store relative path references in your database. My rule of thumb is that if I'm dealing with thumbnail images, SQLite blobs are fine, but for anything larger, use the file system.

这篇关于更快的UIImage - Base64转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆