Solved

Base64 encode iOS not returning what I expect

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

The release of the iPhone 6 and the iPhone 6 Plus was extremely successful for Apple. However when iOS 8.1 was introduced, many iPhone users complained of quick battery drainage. Without even knowing, you may be unnecessarily using up your battery p…
There is a security feature on iOS devices that is nearly impenetrable when it has been activated.  This article will provide some possible solutions as well as necessary steps to take to ensure you do not end up with a locked device.
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.
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…

751 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