简体   繁体   中英

how to convert Array of UIImage to Array of base64 in swift 3 ?

I have an array of images in my codes that are [UIImage] but I want to convert them to base64 - I couldn't ! - I found similar questions but when I used their answers I received Fatal Error

for i in  0...tinyViewController.imageUpload.count - 1 {
        print(i)


        let imageData = UIImageJPEGRepresentation(tinyViewController.imageUpload[i] , 1)

        let base64String = (imageData! as Data).base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
        print(base64String)

}

Try this function to convert each UIImage to base64 String. I used it in my project. It works perfect for me.

func base64(from image: UIImage) -> String? {
        let imageData = UIImagePNGRepresentation(image)
        if let imageString = imageData?.base64EncodedString(options: .endLineWithLineFeed) {
            return imageString
        }
        return nil
    }

So, do this:

for i in  0...tinyViewController.imageUpload.count - 1 {
        print(i)

        print(base64(from: tinyViewController.imageUpload[i]))

}

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