简体   繁体   English

如何在Swift 3中将UIImage数组转换为base64数组?

[英]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 ! 我的代码中有[UIImage]个图像数组,但我想将它们转换为base64-我做不到! - 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. 尝试使用此函数将每个UIImage转换为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]))

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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