gpt4 book ai didi

uiimage - Swift 3 base64 编码图像以供上传

转载 作者:行者123 更新时间:2023-12-02 14:13:03 25 4
gpt4 key购买 nike

我有一些不错的代码,可以让我拍摄图片和图像,然后将其显示在应用程序中。我还将 profilePic.image 上传到我的数据库(作为 blob),但我意识到这实际上并不是上传图像本身,而只是上传有关图像的数据,因此我无法渲染任何内容,因为没有任何内容可解码。所以我想转换这个函数来获取所选图像并将其编码为 base64 并利用相同的工作上传

func imagePickerController(
_ selectImage: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : AnyObject])
{
let chosenImage = info[UIImagePickerControllerEditedImage] as! UIImage //2
// imageByCroppingToRect()
profilePic.contentMode = .scaleAspectFit //3
profilePic.image = chosenImage //4
dismiss(animated: true, completion: nil) //5
}

最佳答案

您必须将 UIImage 转换为您的网站可以读取的格式,例如jpeg,然后以 Base 64 进行编码。

let jpegCompressionQuality: CGFloat = 0.9 // Set this to whatever suits your purpose
if let base64String = UIImageJPEGRepresentation(chosenImage, jpegCompressionQuality)?.base64EncodedString() {
// Upload base64String to your database
}

注意:在 iOS 12 中,UIImageJPEGRepresentation 被 UIImage 的实例方法 jpegData 取代:

let jpegCompressionQuality: CGFloat = 0.9 // Set this to whatever suits your purpose
if let base64String = chosenImage.jpegData(compressionQuality: jpegCompressionQuality)?.base64EncodedString() {
// Upload base64String to your database
}

请参阅 Apple 文档 UIImageJPEGRepresentationData

关于uiimage - Swift 3 base64 编码图像以供上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39441508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com