let image: UIImage = UIImage(named:"imageView")!
let imageData: NSData = UIImagePNGRepresentation(image)! as NSData
base64String = imageData.base64EncodedString(options: .lineLength64Characters)
print(base64String as Any)
我的问题是变量(图像)为空,但我确定我选择了正确的imageView
答案 0 :(得分:1)
您的代码仅在资产目录中存在图片且资产目录是您的目标成员时才有效。
也许您刚刚将文件添加到项目中而不是资产目录中?在这种情况下,请尝试
guard let url = Bundle.main.url(forResource: "image", withExtension: "jpg") else {
return
}
guard let data = try? Data(contentsOf: url) else {
return
}
print(data.base64EncodedString())