I am getting base64 string with extension and I want to convert base64 string to GIF and display it in ImageView. I am using iOSDevCenters+GIF.swift file. I am getting NSData from string but when data converted in image, its giving nil.Below is my code:
我得到base64字符串扩展名,我想将base64字符串转换为GIF并在ImageView中显示它。我正在使用iOSDevCenters + GIF.swift文件。我从字符串获取NSData但是当在图像中转换数据时,它给出nil.Below是我的代码:
let imageData = profileImageString.data(using: .utf8)
self.thumbnailMedia.image = UIImage.gifImageWithData(imageData!)
Does anybody have any ideas on how to do this?
有没有人对如何做到这一点有任何想法?
1 个解决方案
#1
0
If you are starting from a base64 string, you should decode it as a base64 string not UTF8
.
如果从base64字符串开始,则应将其解码为base64字符串而不是UTF8。
if let data = Data(base64Encoded: imageDataString) {
let image = UIImage(data: data)
}
This snippet simply takes the encode image string, decode into a Data
object and create an image from the data.
If you are working a lot using base64 string I strongly suggest you to extend the String
structure functionalities.
此片段只需获取编码图像字符串,解码为Data对象并根据数据创建图像。如果你使用base64字符串工作很多,我强烈建议你扩展String结构的功能。
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) {
return String(data: data, encoding: .utf8)
}
return nil
}
}
This snippet was taken from Github, credits to Stringer.
这个片段取自Github,归功于Stringer。
Also another way is use the extension created by Leo Dabus that is compliant with Swift convention:
另一种方法是使用Leo Dabus创建的符合Swift约定的扩展:
extension String {
var data: Data { return Data(utf8) }
var base64Encoded: Data { return data.base64EncodedData() }
var base64Decoded: Data? { return Data(base64Encoded: self) }
}
extension Data {
var string: String? { return String(data: self, encoding: .utf8) }
}
#1
0
If you are starting from a base64 string, you should decode it as a base64 string not UTF8
.
如果从base64字符串开始,则应将其解码为base64字符串而不是UTF8。
if let data = Data(base64Encoded: imageDataString) {
let image = UIImage(data: data)
}
This snippet simply takes the encode image string, decode into a Data
object and create an image from the data.
If you are working a lot using base64 string I strongly suggest you to extend the String
structure functionalities.
此片段只需获取编码图像字符串,解码为Data对象并根据数据创建图像。如果你使用base64字符串工作很多,我强烈建议你扩展String结构的功能。
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) {
return String(data: data, encoding: .utf8)
}
return nil
}
}
This snippet was taken from Github, credits to Stringer.
这个片段取自Github,归功于Stringer。
Also another way is use the extension created by Leo Dabus that is compliant with Swift convention:
另一种方法是使用Leo Dabus创建的符合Swift约定的扩展:
extension String {
var data: Data { return Data(utf8) }
var base64Encoded: Data { return data.base64EncodedData() }
var base64Decoded: Data? { return Data(base64Encoded: self) }
}
extension Data {
var string: String? { return String(data: self, encoding: .utf8) }
}