1

I am getting a byte array(I reckon) in NSMutableArray's object.The NSMutableArray i get is from Xml parsing from a service URL..Now I need to convert that byte array to UIImage ..Gone through google and found to convert to NSData and then UIImage but couldn't understand how..? How can I do it ?

My array looks like this :

(
{
        Name = "John";
        Image = "/9j/4AAQSkZJRgABAQAAAQABAAD//gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJS
kcgSlBFRyB2NjIpLCBxdWFsaXR5ID0gODAK/9sAQwAGBAUGBQQGBgUGBwcGCAoQCgoJCQoUDg8MEBcUGBgXF
BYWGh0lHxobIxwWFiAsICMmJykqKRkfLTAtKDAlKCko/9sAQwEHBwcKCAoTCgoTKBoWGigoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgo/8AAEQgEKwZAAwEiAAIRAQMRAf/........
 //big byte array 
}
)

First of all I need to clear whether it is a byte array ?How can I extract image from this ?

1 Answer 1

5

Your data is organized like this:

  • The out-most data structure is a NSArray or NSMutableArray (as you write).
  • At index 0 (first element in array), there's an NSDictionary instance.
  • In the dictionary, there are two key-value pairs. One has the key Image and a string as the value.
  • The string value is binary data, encoded as Base64.
  • The binary data is the image data.

So the get the image, you need to go this path:

NSMutableArray* array = ... // from XML
NSDictionary* dict = [array objectAtIndex: 0];
NSString* dataString = [dict objectForKey: @"Image"];
NSData* imageData = [dataString base64DecodedData];
UIImage* image = [UIImage imageWithData: imageData];

Note that the Base64 decoding is not part of iOS. But there are many implementations on the net for decoding a string into data. Just google it.

A Base64 implementation that looks good and simple can be found on GitHub.

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

4 Comments

but example in the link u provided is not running in my mac(10.7.4) with xcode 4.1
docs there: Supported build target - iOS 5.1 / Mac OS 10.7 (Xcode 4.3, Apple LLVM compiler 3.0) -- you should update -- Id even say for any serious work, go to xcode 4.5.2+ (else, find a different implementation - as godo said: there are many! the dropbox sdk has a nice one too)
getting this error:target specifies product type 'com.apple.product-type.tool', but there's no such product type for the 'iphonesimulator' platform
You have probably added far too many files to your project. Just add the four files in the Base64 directory to your existing iPhone app project and import NSString+Base64.h into the class where you decode the Base64 data.

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.