1

I have converted my images with this codes below and this will return base64 of my images but the problem is that when I upload them into the server I just see the white image - can you suggest me the better way to convert images to base64 or this is the best way that I choose here is the converting image to base64

 func base64(from image: UIImage) -> String? {
    UIGraphicsBeginImageContext(image.size)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    let imageData = UIImageJPEGRepresentation(image!, 1.0)
    if let imageString = imageData?.base64EncodedString(options: .endLineWithLineFeed) {
        return imageString
    }
    return nil
}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
Saeed Rahmatolahi
  • 1,317
  • 2
  • 27
  • 60
  • one thing you can do is construct the image back from the base64 string which is generated , if that works check compress the image and check whether the string is properly sent, since base64 string size is too large. – yoshiiiiiiii Jul 31 '17 at 04:50

4 Answers4

1

Try this method:

func convertImageToBase64(_ image:UIImage) -> String
  {
    let imageData = UIImageJPEGRepresentation(image, 1)//get imageData from image
    if(imageData != nil)
    {
      let Strbase64 = imageData?.base64EncodedString(options: .endLineWithCarriageReturn)//encode imageData
      return Strbase64!//return encoded string
    }
    return ""
  }
Brijesh Shiroya
  • 3,323
  • 1
  • 13
  • 20
1

You passed a UIImage to this method, but never did anything with it, but rather created a new UIImage using UIGraphicsGetImageFromCurrentImageContext, but you never drew anything to that context. That's why the resulting images are white.

But you don't need those UIGraphics calls at all. Just grab the JPEG representation of the UIImage that you passed into this method, and convert it to a base 64 encoded string:

func convertImageToBase64(_ image: UIImage) -> String? {
    return UIImageJPEGRepresentation(image, 1)?.base64EncodedString()
}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • This is not working look up the answer is working but not for more than one image – Saeed Rahmatolahi Jul 31 '17 at 05:49
  • Like I said in my comment to the other question, the problem with multiple images is not this conversion to base64. The above is how you do that conversion and there's not much to it. The multiple image problem is either how you're grabbing these multiple `UIImage` references or how you're saving them or something like that. But you haven't provided enough in your question to help us diagnose this secondary, multiple image problem. – Rob Jul 31 '17 at 05:59
  • @SaeedRahmatolahi - By the way, in comments to another answer, you said you're taking pictures from the phone. If using `UIImagePickerController`, for example, it's inefficient to grab the `UIImage` objects, convert them to `Data`/`NSData` and then doing the base 64 conversion. You generally would want to grab the original `Data`/`NSData` asset (like https://stackoverflow.com/a/32938728/1271826). But without more context, we can't help you. – Rob Jul 31 '17 at 06:02
0

It is white because nothing is drawn on current context !!!

You are just UIGraphicsBeginImageContext with size

and get with UIGraphicsGetImageFromCurrentImageContext

it will always gives you a white image and there is no relation with Base64

If you want to render something then use like below

        yourImageView.layer.render(in: UIGraphicsGetCurrentContext()!)

and after that fetch image from currentContext

EDIT

func base64(from image: UIImage) -> String? { 
   UIGraphicsBeginImageContext(image.size) 
   image.draw(in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)) 
   let image = UIGraphicsGetImageFromCurrentImageContext() 
   UIGraphicsEndImageContext() 
   let imageData = UIImageJPEGRepresentation(image!, 1.0) 
   if let imageString = imageData?.base64EncodedString(options: .endLineWithLineFeed) { 
   return imageString 
   } 
    return nil 
  }
Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98
0

First check wether the error you are facing is from your app or server side .You can choose online base64 to image converter .Copy your base64 String and check them first , you can choose this link

https://codebeautify.org/base64-to-image-converter

Subhojit Mandal
  • 450
  • 4
  • 13