Solved

Base64 encode iOS not returning what I expect

Posted on 2012-03-23
2
1,298 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Are you having trouble connecting or getting your iPhone / Samsung device(s) to sync with Microsoft Exchange Server?   What have you tried?   What haven't you tried?
Let Bitmoji into your life. Now is the time to learn a new language of smartphone messaging with this brief introduction.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now