4

Currently, I'm trying to upload a base64 encoded image to a php server which is then storing the base64 string in a MySQL database. Currently, the code is uploading the data and storing it into the MySQL database. However, when I attempt to retrieve the image by specifying the URL used to retrieve the image, a missing image link with the question mark is shown. I have no idea why this is happening since both uploading and displaying base64 encoded images seems to be working just fine with my Android app.

Here is the Swift code I'm using to encode and upload to the server:

    let image: UIImage = imgProfilePic.image!

    let size = CGSizeApplyAffineTransform(image.size, CGAffineTransformMakeScale(0.3, 0.3))
    let hasAlpha = false
    let scale: CGFloat = 0.0 // Automatically use scale factor of main screen

    UIGraphicsBeginImageContextWithOptions(size, !hasAlpha, scale)
    image.drawInRect(CGRect(origin: CGPointZero, size: size))

    let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    var imageData = UIImageJPEGRepresentation(scaledImage, 0.9)
    var base64String = imageData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0)) // encode the image

    var cd = CoreDataUser(pstrContext: "this")

    var params = "strUsername=" + cd.getUsername()
    params = params + "&strPassword=" + cd.getPassword()
    params = params + "&blbProfilePic=" + base64String

Here is the PHP code where the base64 string is being decoded and displayed in the browser. This is working fine for images that are uploaded by my Android code, but it just shows a broken image link for images uploaded by my Swift code.

 if ($rows) { 
    foreach ($rows as $row) { 
    $data = base64_decode($row["fblbProfilePic"]);
    $image = imagecreatefromstring($data);
    header('Content-Type: Image/jpeg');
    imagejpeg($image);
//file_put_contents("test.jpg", $data);
//var_dump($data);

    //echo base64_decode($row["fblbPicture"]);
    /    /echo '<img src="data:image/jpg;base64,' . $row["fblbPicture"]     . '" />';
     }
19
  • Please share the code where you are downloading and decoding it to base64.. entire code from webservice call. Commented Apr 11, 2015 at 19:40
  • Thanks for the response, Arun. I edited my question to contain the PHP code where the base64 image string is being decoded and added displayed in the browser. Commented Apr 11, 2015 at 19:47
  • What you can try is call the same service from iPhone,extract the imagestring decode it and display it in imageView. If this works then you are sending it right. Only issue would be the way encoding is happening as in iOS we are using NSDataBase64EncodingOptions whose equivalent needs to be seen in php. Commented Apr 11, 2015 at 19:55
  • Thanks again for the response. I actually did exactly what you recommend here. I encoded the image into a base64 string in Swift, then I took the encoded string and base64 decoded it in Swift and was able to successfully display it in another UIImageView. Something seems to be happening at the server to cause a corruption in the base64 string or something. I'm at a loss in figuring it out at the moment. Commented Apr 11, 2015 at 19:58
  • Did you took the encoded image from server or decoded it locally in iOS? Is your server returning the image directly as data or in json key valur pair? Commented Apr 11, 2015 at 20:00

3 Answers 3

2

I was able to get this working by percent encoding the base64 string before posting it to the PHP server. Hope this helps someone else.

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

1 Comment

Thanks! I was having problems with this, your post helped!
2

Just supplying some helpful Swift 3.0 code here on the back of play2win's self-answer:

let data:Data = UIImagePNGRepresentation(myUIImageView.image!)!
let base64String:String = data.base64EncodedString(options: Data.Base64EncodingOptions(rawValue: 0))
let imageStr:String = base64String.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!

Comments

0

You should first decode your base64 image in to data and save on server side so that we will get that saved location path of your in response that you wanted.. hope this help you...

1 Comment

Thank you for the response. The data for the image is getting saved in the MySQL database. I'm using the same steps in Swift that I used for the Android app and the Android app is working fine. First I get the image, then I encode the image into a base64 string, then I send it to the server which stores the base64 string in the database. When I retrieve the image, I select it from the database and decode the base64 string with PHP and display the decoded image in the browser which is then picked up by the app. For some reason the Swift code isn't working properly, though.

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.