Base64Encoded string in ios not decoding properly on server side using php - iphone

I am converting image to NSdata using UIImageJPEGRepresentation(image, 1.0) and then encoding it to base64 using base64 helper class
NSString *imageOne = [self encodeBase64WithData:[imageDict objectForKey:#"ImageOne"]];
and finally sending it to server using post method as json parameter and in server side using php method to decode it.
/* encode & write data (binary) */
$ifp = fopen($imageNameWithPath, "wb" );
fwrite($ifp, base64_decode($base64ImageString));
fclose($ifp);
After decoding it, I am saving the file as jpeg image and the file is created with proper size and extension but problem is that when I open it, I get the DIMENSION OF IMAGE as 0X0 ..(problem is here)
Server side script is correct as our android developer is also sending base64string and the image is saved as jpeg with proper size and dimension.
This problem is only from iphone side when sending to server for decoding. I have decoded the image using my base64 helper class and it works fine on my iPhone device.
UIImageView *viewImage = [[UIImageView alloc] initWithImage:[UIImage imageWithData: [self decodeBase64WithString:[registerDataDict objectForKey:#"imageOne"]]]];
[self.view addSubview:viewImage];
Is the process followed by me correct on the device or do I need to change something? Help would be appreciated.

There are variants on the extra chars used to get to the 65 needed for base64 encoding (26 uppercase + 26 lowercase + 10 digits = 62), and different encoders may use a different subset. PHP is expecting them to be ['+', '/', '='], your application may be using something like ['-', '_', '='].
Once you figure out what the mapping is you can use str_replace before the decode in php.
See the wikipedia page on base64 or the php documentation for base64_decode for more info.

I'm successfully using this with several OAuth providers: Base64Transcoder.c It's a C class, you can easily turn it into a Objective-C class if you like.
I'm encoding like this:
char base64Result[32];
size_t theResultLength = 32;
[Base64Transcoder base64EncodeData:digest digestLength:CC_SHA1_DIGEST_LENGTH base64Result:base64Result resultLength:&theResultLength];
NSData *theData = [NSData dataWithBytes:base64Result length:theResultLength];
NSString *base64EncodedResult = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];
Hmm well, I turned the C function base64EncodeData of the original class into an Objective-C method but you get the idea.

Related

Convert image data to binary text for vcard

I need to include an image in a vcard file. The image is supposed to be in binary format. I create the image data as follows:
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:#"Pic1"], 1.0);
I have unsuccessfully tried encoding using the following 2 methods:
vcardString = [vcardString stringByAppendingFormat:#"PHOTO;ENCODING=b;TYPE=JPEG:%#\n", [imageData base64EncodedString]];
AND
vcardString = [vcardString stringByAppendingFormat:#"PHOTO;ENCODING=b;TYPE=JPEG:%#\n", [imageData description]];
Any advice on how to get the image data properly encoded to binary would be appreciated. Thanks
According to this blog entry I found (which points to this spec), it looks like the second form of what you're trying to do should work.
Change your ENCODING=b to ENCODING=BASE64 and see if that makes the difference.
OS X v10.11 and iOS 9 introduces CNContactVCardSerialization which greatly simplifies embedding images in a VCard.

iOS support for Canon RAW format?

I know the iPad can read Canon RAW (.CR2) files when used with the Camera Kit but is any of that file format reading accessible to an iOS developer? Or are we just limited to things supported in the UIImage Class Reference document?
(https://developer.apple.com/documentation/uikit/uiimage)
For something like this, Apple's image APIs are not going to be sufficient for what you need. However, there are a few third party APIs that should work with the iPhone. Libraw is a good one that I found that has a C interface. Since Objective-C is a strict superset of C, you will probably be able to use it in your iOS application.
From what I see in their docs, you might be able to write a CR2 image as a TIFF, then decode it as a UIImage like this:
NSString * cr2Path = #"path/to/cr2/file";
NSString * tiffPath = [NSTemporaryDirectory stringByAppendingPathComponent:#"x.tiff"];
libraw_data_t * cr2Data = libraw_init(0);
libraw_open_file(cr2Data, [cr2Path UTF8String]);
libraw_unpack(cr2Data);
// setup encoding params
cr2Data->params.output_tiff = 1;
cr2Data->params.use_camera_wb = 1;
// encode and write as tiff
libraw_dcraw_process(cr2Data);
libraw_dcraw_ppm_tiff_writer(cr2Data, [tiffPath UTF8String]);
libraw_recycle(cr2Data); // free the memory
// load the UIImage
UIImage * myImage = [[UIImage alloc] initWithContentsOfFile:tiffPath];
// use myImage here ...
[myImage release];
From what I have seen, it looks like you will be able to download it here and then add the src files from the src/, libraw/, dcraw/, and internal/ directories to your Xcode project. Hopefully you will not run into any weird linking/compiling issues.

Converting a photo data to nsstring returns nil

I'm allowing the users of my app to either take a pic or select one from their library. When selected I need to get the images' data and convert it to a string so I can send it to a web service.
The problem I'm currently having is that [NSString initWithData:] is returning nil when I have the encoding set to UTF8. I need to set it to UTF8 for the XML message.
NSString *dataString = [[NSString alloc] initWithData:UIImageJPEGRepresentation(theImage, 1.0) encoding:NSUTF8StringEncoding];
Thanks for any advice!
You can't convert an UIImage to NSString by using initWithData:encoding: method. This method is only for converting an string's data to NSString (an Text File for example).
If you are trying to convert any kind of binary data to NSString, there are some kind of encoding available. Base64 is widely used. Also your server should have the ability to decode what you've sent.
In addition, in most cases, send an image to server just need to POST it in binary as it used to be.

NSData to NSString using initWithBytes:length:encoding

I have some image data (jpeg) I want to send from my iPhone app to my webservice. In order to do this, I'm using the NSData from the image and converting it into a string which will be placed in my JSON.
Currently, I'm doing this:
NSString *secondString = [[NSString alloc] initWithBytes:[result bytes]
length:[result length]
encoding:NSUTF8StringEncoding];
Where result is of type NSData. However, secondString appears to be null even though result length returns a real value (like 14189). I used this method since result is raw data and not null-terminated.
Am I doing something wrong? I've used this code in other areas and it seems to work fine (but those areas I'm currently using it involve text not image data).
TIA.
For binary data, better to encode it using Base64 encoding then decode it in you webservice. I use NSData+Base64 class downloaded from here, this reference was also taken from Stackoverflow, an answer made by #Ken (Thanks Ken!).
You are not converting the data to a string. You are attempting to interpret it as a UTF-8 encoded string, which will fail unless the data really is a UTF-8 encoded string. Your best bet is to encode it somehow, perhaps with Base64 as Manny suggests, and then decode it again on the server.

Convert NSData [UIImage] to a NSString

I am aware this question has been asked several times, but I was unable to find a definate answer that would best fit my situation.
I want the ability to have the user select an image from the library, and then that image is converted to an NSData type. I then have a requirement to call a .NET C# webservice via a HTTP get so ideally I need the resulting string to be UTF8 encoded.
This is what I have so far:
NSData *dataObj = UIImageJPEGRepresentation(selectedImage, 1.0);
[picker dismissModalViewControllerAnimated:YES];
NSString *content = [[NSString alloc] initWithData:dataObj encoding:NSUTF8StringEncoding];
NSLog(#"%#", content);
The NSLog statement simply produces output as:
2009-11-29 14:13:33.937 TestUpload2[5735:207] (null)
Obviously this isnt what I hoped to achieve, so any help would be great.
Kind Regards
You can't create a UTF-8 encoded string out of just any arbitrary binary data - the data needs to actually be UTF-8 encoded, and the data for your JPEG image obviously is not. Your binary data doesn't represent a string, so you can't directly create a string from it - -[NSString initWithData:encoding:] fails appropriately in your case.
Assuming you're using NSURLConnection (although a similar statement should be true for other methods), you'll construct your NSMutableURLRequest and use -setHTTPBody: which you need to pass an NSData object to. I don't understand why you would be using a GET method here since it sounds like you're going to be uploading this image data to your web service - you should be using POST.