Base64图像编码Swift 4 iOS [英] Base64 image encoding Swift 4 iOS

查看:111
本文介绍了Base64图像编码Swift 4 iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码从库中选择一个图像,然后使用base64encode进行将来的上载...已选择该图像并根据需要显示在应用中...但是在输出中出现此错误

I have the following code to select an image from the library and then base64encode for future upload... image is selected and appears in app as I want... however in output I get this error

发现扩展时遇到[发现]错误:错误域= PlugInKit代码= 13查询已取消" UserInfo = {NSLocalizedDescription =查询已取消}

[discovery] errors encountered while discovering extensions: Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled}

,如果我在输出中使用打印的base64代码并使用网络工具( https://www.base64decode.org/)尝试解码上述信息,以确认它是否起作用,然后再开始在iOS中开始进行解码-它似乎格式错误?我假设我没有正确编码图像吗?

and if I take the printed base64 code in output and use a webtool (https://www.base64decode.org/) to attempt to decode said info, to confirm it has worked, before I start to work on decoding in iOS later - it appears to be malformed ? I am assuming I am not correctly encoding the image still ?

 @IBAction func selectImage(_ sender: AnyObject) {


        selectImage.allowsEditing = true //2
        selectImage.sourceType = .photoLibrary //3
        present(selectImage, animated: true, completion: nil)//4


    }

  func imagePickerController(_ selectImage: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {


        let chosenImage = info[UIImagePickerControllerEditedImage] as! UIImage //2

        profilePic.contentMode = .scaleAspectFit //3
        profilePic.image = chosenImage //4

        let quality = 1.0
        base64String = (UIImageJPEGRepresentation(chosenImage, CGFloat(quality))?.base64EncodedString())!
        print (base64String)

        self.dismiss(animated: true, completion: nil) //5
    }

推荐答案

我在我的项目中使用了这两个函数,并且运行正常.

I'm using these 2 functions in my project and it is working fine.

 func imageTobase64(image: UIImage) -> String {
        var base64String = ""
        let  cim = CIImage(image: image)
        if (cim != nil) {
            let imageData = image.highQualityJPEGNSData
            base64String = imageData.base64EncodedString(options: NSData.Base64EncodingOptions.lineLength64Characters)
        }
        return base64String
    }

    func base64ToImage(base64: String) -> UIImage {
        var img: UIImage = UIImage()
        if (!base64.isEmpty) {
            if let decodedData = Data(base64Encoded: base64 , options: NSData.Base64DecodingOptions.ignoreUnknownCharacters) as Data {
let decodedimage = UIImage(data: decodedData)
            img = (decodedimage as UIImage?)!
}

        }
        return img
    }

我还有一个扩展程序来处理可能有用的图像质量:

I have also an extension to handle image quality that can be useful:

  extension UIImage {
            var highestQualityJPEGNSData:NSData { return UIImageJPEGRepresentation(self, 1.0)! as NSData }
            var highQualityJPEGNSData:NSData    { return UIImageJPEGRepresentation(self, 0.75)! as NSData}
            var mediumQualityJPEGNSData:NSData  { return UIImageJPEGRepresentation(self, 0.5)! as NSData }
            var lowQualityJPEGNSData:NSData     { return UIImageJPEGRepresentation(self, 0.25)! as NSData}
            var lowestQualityJPEGNSData:NSData  { return UIImageJPEGRepresentation(self, 0.0)! as NSData }
        }

//对于Swift 4.2-修改后的扩展

//For Swift 4.2 - modified extension

extension UIImage {
    var highestQualityJPEGNSData:NSData { return self.jpegData(compressionQuality: 1.0)! as NSData }
    var highQualityJPEGNSData:NSData    { return self.jpegData(compressionQuality: 0.75)! as NSData}
    var mediumQualityJPEGNSData:NSData  { return self.jpegData(compressionQuality: 0.5)! as NSData }
    var lowQualityJPEGNSData:NSData     { return self.jpegData(compressionQuality: 0.25)! as NSData}
    var lowestQualityJPEGNSData:NSData  { return self.jpegData(compressionQuality: 0.0)! as NSData }
}

这篇关于Base64图像编码Swift 4 iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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