7

I am trying to convert the Image which is picked by user either from his Photos or Take New from Camera. I am able to convert the image into base64 string but the problem is that it takes too much time and prints a long infinite string

Here is the output of String which i am getting

enter image description here

here is my code:

// Image picker from Gallery
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
        imagePicker.dismissViewControllerAnimated(true, completion: nil)
        profileImage.image = image

    }

    // Image Picker from Camera

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        imagePicker.dismissViewControllerAnimated(true, completion: nil)
        profileImage.image = info[UIImagePickerControllerOriginalImage] as? UIImage

        addPicBtn.setImage(nil, forState: .Normal)

        let imageData:NSData = UIImagePNGRepresentation(profileImage.image!)!
        let imageStr = imageData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
        print(imageStr)


    }
3
  • Since it is converting it is taking the time, it should not effect while uploading the image with base64 text. Commented Dec 6, 2016 at 6:30
  • what you want to do now ?? i mean where are you facing problem ...?? you can check Base64 string from below link , if it is encoded right or not codebeautify.org/base64-to-image-converter Commented Dec 6, 2016 at 6:30
  • @dhiru its taking a lot of time while converting it into an string Commented Dec 6, 2016 at 6:35

5 Answers 5

13

Actually it will not take time to convert but for printing, it will take more time so don't print it...

Sign up to request clarification or add additional context in comments.

9 Comments

i want this string to be added in database
you add directly. It won't take much time.
but when i suppose to pick image from gallery, application holds there for a few mins
If you want less quality image than use UIImageJPEGRepresentation(imagePicked, 0.5)! than string value will come less, for loading use dispatch_async method and show loading spinner till image not come...
can you tell me how to decode the string into image
|
9

You can apply this code

let imageData: Data? = UIImageJPEGRepresentation(getImage(), 0.4)
let imageStr = imageData?.base64EncodedString(options: .lineLength64Characters) ?? ""
print(strBase64)

1 Comment

this will reduce the quality of images
3

Swift 4 version. This simple func worked well for me. Confirmed decoded image back using this: https://codebeautify.org/base64-to-image-converter Hope this helps someone.

public static func  convertImageToBase64String(image : UIImage ) -> String 
{
    let strBase64 =  image.pngData()?.base64EncodedString()
    return strBase64!
}

Comments

0

Make sure your image extension first.

// .png

    guard let imageData = UIImagePNGRepresentation(UIImage) else {
        return ""
    }

// .JPEG

   guard let imageData = UIImageJPEGRepresentation(UIImage, 1) else {
                return ""
    }

// BASE 64

  imageData.base64EncodedString()

Comments

0
let imageData: Data? = UIImageJPEGRepresentation(YourImage, 0.4)
let imageStr = imageData?.base64EncodedString(options: .lineLength64Characters) ?? ""
print(imageStr,"imageString")

After printing imageStr you will huge output like in this attached image below, it means you are done successfully.

1 Comment

this output means that we have successfully converted image into string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.