将图像编码为 base64 时的字符串无效 - Swift 4
Invalid string in encoding image to base64 - Swift 4
我的问题是当我尝试将图像编码为 base-64 时,它会生成一个长字符串(最多 200,000 行)并且在解码时不起作用,它会生成一个 nil 图像!即使我尝试将图像调整为更小的尺寸,但仍然不起作用!
这是我的图片编码代码
let image = imageView.image
let imageData:NSData = UIImagePNGRepresentation(image!)! as NSData
let strBase64 = imageData.base64EncodedString(options: .lineLength64Characters)
解码代码:
let processImage = user.value(forKey: "processImage") as! String // image from json
if let dataDecoded:NSData = NSData(base64Encoded: processImage, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters) {
let image: UIImage = UIImage(data:dataDecoded as Data,scale:1.0)!
print(image.size)
self.myImage.image = image }
您可以尝试以下步骤:
步骤:- 1 首先从中获取 NSData
存在三种可能性:
1.使用图像名称
let image : UIImage = UIImage(named:"imageNameHere")!
//Now use image to create into NSData format
let imageData:NSData = UIImagePNGRepresentation(image)!
2。使用 NSURL:
//Use image's path to create NSData
let url:NSURL = NSURL(string : "urlHere")!
//Now use image to create into NSData format
let imageData:NSData = NSData.init(contentsOfURL: url)!
3。从图像选择器中选取的图像
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
let imageData:NSData = UIImagePNGRepresentation(image)!
步骤:-2 然后编码:
let strBase64 = imageData.base64EncodedString(options: .lineLength64Characters)
print(strBase64)
步骤:-3 然后解码:
let dataDecoded : Data = Data(base64Encoded: strBase64, options: .ignoreUnknownCharacters)!
let decodedimage = UIImage(data: dataDecoded)
yourImageView.image = decodedimage
希望对您有所帮助。
我的问题是当我尝试将图像编码为 base-64 时,它会生成一个长字符串(最多 200,000 行)并且在解码时不起作用,它会生成一个 nil 图像!即使我尝试将图像调整为更小的尺寸,但仍然不起作用!
这是我的图片编码代码
let image = imageView.image
let imageData:NSData = UIImagePNGRepresentation(image!)! as NSData
let strBase64 = imageData.base64EncodedString(options: .lineLength64Characters)
解码代码:
let processImage = user.value(forKey: "processImage") as! String // image from json
if let dataDecoded:NSData = NSData(base64Encoded: processImage, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters) {
let image: UIImage = UIImage(data:dataDecoded as Data,scale:1.0)!
print(image.size)
self.myImage.image = image }
您可以尝试以下步骤:
步骤:- 1 首先从中获取 NSData
存在三种可能性:
1.使用图像名称
let image : UIImage = UIImage(named:"imageNameHere")!
//Now use image to create into NSData format
let imageData:NSData = UIImagePNGRepresentation(image)!
2。使用 NSURL:
//Use image's path to create NSData
let url:NSURL = NSURL(string : "urlHere")!
//Now use image to create into NSData format
let imageData:NSData = NSData.init(contentsOfURL: url)!
3。从图像选择器中选取的图像
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
let imageData:NSData = UIImagePNGRepresentation(image)!
步骤:-2 然后编码:
let strBase64 = imageData.base64EncodedString(options: .lineLength64Characters)
print(strBase64)
步骤:-3 然后解码:
let dataDecoded : Data = Data(base64Encoded: strBase64, options: .ignoreUnknownCharacters)!
let decodedimage = UIImage(data: dataDecoded)
yourImageView.image = decodedimage
希望对您有所帮助。