Solved

Base64 encode iOS not returning what I expect

Posted on 2012-03-23
2
1,362 Views
Last Modified: 2016-02-10
I'm trying to base64 encrypt an image for upload on iOS, but the stupid thing doesn't have built in support, so I downloaded two libraries and tried them both out, but I think they are returning the wrong values (unless I understand base64 even less than I think I do).  This website returns a long string that starts with this:  

/9j/4Q+zRXhpZgAASUkqAAgAAAAKAA4BAgAgAAAAhgAAAA8BAgAFAAAApgAAABABAgAKAA

Open in new window


When I encrypt using the library I downloaded, it starts with this:

iVBORw0KGgoAAAANSUhEUgAAAI0AAABnCAIAAACy41YWAABAAElEQVR4AbzdeaylyXUY9nf3

Open in new window


I am 100% sure the first one is correct, because when I use a test program that was written to test the server, that's the same code that it uploads to the server and the server happily decodes the file and saves the image. Here is the test image.

Basically, what I'm thinking is that I need to get a different library for the iPhone / iPad. Another odd thing I noticed is that for some images, the = will be at the end of the string, and for others it won't be. There are three reasons I can think of for this.

1) the iPhone is running out of memory and doesn't want to tell me so it just uploads what it has.

2) the library is broken

3) I'm confused and don't understand base64

Any suggestions?

EDIT: The iPhone is actually the simulator, so I don't know if it will run out of memory before the computer does. I know the android emulator doesn't, but I'm not sure about the iOS simulator.
0
Comment
Question by:HDM
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 500 total points
ID: 37760091
I cannot give you my code.

The shortest working code is here:
http://www.cocoadev.com/index.pl?BaseSixtyFour

In Github you can find a longer solution:
https://github.com/mikeho/QSUtilities

or NSData category:
https://github.com/mattgemmell/MGTwitterEngine/blob/master/NSData+Base64.h
https://github.com/mattgemmell/MGTwitterEngine/blob/master/NSData+Base64.m

I found this blog post with the attached code and examples:
iPhone SDK base64 encode / decode
0
 

Author Closing Comment

by:HDM
ID: 37766608
No matter which library I used I would have run into the same thing. I was telling it to use a png image as a representation even though the image was a jpeg, which was what was causing the different values for base64, which makes a lot of sense.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let Bitmoji into your life. Now is the time to learn a new language of smartphone messaging with this brief introduction.
Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question