简体   繁体   中英

Base64 image encoding Swift 4 iOS

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

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

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 }
        }

//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 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM