I am trying to create a UIImage from a byte array that is actually held within a NSString.
Can someone please tell me how I can do that?
Here is what I was thinking of doing:
NSString *sourceString = #"mYActualBytesAREinHERe=";
//get the bytes
const char *bytesArray = [sourceString cStringUsingEncoding:NSASCIIStringEncoding];
//build the NSData object
NSData *data = [NSData dataWithBytes:bytesArray length:[sourceString length]];
//create actual image
UIImage *image = [UIImage imageWithData:data];
The problem is image is always 0x0 (nil).
Any suggestions would be appreciated.
Thanks!
To convert an image to string you need a method to convert NSData to a base64Encoded string and back (lots of examples here). The easiest ones to use are categories on NSData so you can do something like this:
UIImage* pic = [UIImage imageNamed:#"sample.png"];
NSData* pictureData = UIImagePNGRepresentation(pic);
NSString* pictureDataString = [pictureData base64Encoding];
To go the other way you need a reverse converter:
UIImage* image = [UIImage imageWithData:[NSData
dataFromBase64EncodedString: pictureDataString]];
[UIImage imageWithData:data]; will return nil if it doesn't understand the data being passed to it. I would double check your encoding, etc. It's odd that a binary string would hold pure image data without some kind of encoding (base64, etc.). String encodings and binary encodings aren't compatible.
I bet your image data has some null characters in there (0x00) and as you know that is the terminator for the string, so when you ask for the C string, you probably get way-way too little data.
Try something like
- (NSData *)dataUsingEncoding:(NSStringEncoding)encoding;
to generate your NSData.
If that doesn't work you need to evaluate whether the setting the data into an NSString (with embedded null chars) isn't causing a loss of data too.
Like one of the other respondents, perhaps base-64 encoding your data would be a good idea (if using a string to transport the img data is a requirement)
Good luck.
Related
I am able to get the base 64 string of an image using the following code,
UIImage *thumbImage = [UIImage imageNamed:#"image.png"];
NSData *theThumbImageData = UIImageJPEGRepresentation(thumbImage,0.1);
NSString *str = [theThumbImageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
Now i am trying to get the image back from base64 string and below code is used for that purpose.
NSString *string = #"base64 encoded string retrieved from server";
NSData *base64Data = [imageData dataUsingEncoding:NSUTF8StringEncoding];
UIImage* theMultimediaImage = [[UIImage alloc] initWithData:[base64Data base64EncodedDataWithOptions:0]];
The problem am facing here is that during the image conversion from base64 string am getting NIL value.
Please help me to rectify my mistake.
Note: Followed the link ,Converting NSString to NSData and vice versa
Regards,
Bhat
In the first line of code you are initializing an NSString variable named 'string' but in the 2nd line of code you are trying to extract data from a different (presumably string) variable named 'imageData'.
In my application when i try with following coding i get the warning as
Code
[UIImageJPEGRepresentation(petAvadar.image, 1.0)base64Encoding]
WARNING
Instance method '-base64Encoding' not found (return type defaults to 'id')
How to remove this warning,Please help me to solve.
Then convert your UIImage object into NSData the following way:
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
And then apply Base64 encoding to convert it into a base64 encoded string:
NSString *encodedString = [imageData base64Encoding];
once check this one
You need to declare your function in the header file.
You should add a line like this :
-(returnType)base64Encoding;
Where returnType is the type returned by your method, like NSString*, NSInteger, void or whatever your method returns.
Make sure that you have donlaoded Base64 lib classes from here
then import Base64.h use below code snip
For Encoding Image to Base64
NSData* data = UIImageJPEGRepresentation(yourImage, 1.0f);
[Base64 initialize];
NSString *strEncoded = [Base64 encode:data];
and Decode Base64 as image:
[Base64 initialize];
NSData* data = [Base64 decode:strEncoded ];;
image.image = [UIImage imageWithData:data];
Alos you may like to check this and this link as well
Is it possible to convert NSData/UIImage Data Representation as JPEG to a String, to be sent over HTTP to a PHP File to save this string in a database, and then retrive it later on in the application and convert it back into an NSData/UIImage Object?
I have tried Base64 Encoding Libraries but base64 doesn't seem valid as the image doesn't display correctly on a HTML Page.
Any suggestions?
Edit.
I was using the following library:
http://www.imthi.com/blog/programming/iphone-sdk-base64-encode-decode.php
And converting in the following way:
NSData *imageData = UIImageJPEGRepresentation(MyImage.image, 90);
[Base64 initialize];
NSData *encoded = [Base64 encode:imageData];
NSLog(#"%#",encoded);
This does chug out alot of BASE64 but when I save it to a file and try to view it, I just get the eror loading image [?] in Chrome.
Thanks
The point of encoding an NSData object to base 64 is so you can represent the data as a string that can be stored or transferred more easily. You then need to decode the base 64 encoded string back into NSData. This data can then be used to create a new UIImage. Your server needs to do this decoding to get back the original data.
Your code has a mistake. This line:
NSData *encoded = [Base64 encode:imageData];
should be:
NSString *encoded = [Base64 encode:imageData];
Notice that you get back a string, not data.
You commented that you wrote the encoded string to a file then couldn't view the image. Of course not. If you want to write the image data to a file so the file is actually viewable as the image, then don't encode the data first. Write the raw image data to a file.
you can convert image to string like this
first convert your UIImage to NSData & then convert that ata into string by using encodeBase64WithData
NSString *imageOne = [self encodeBase64WithData:[imageDict objectForKey:#"ImageOne"]];
and again string to UIImage like this:
[UIImage imageWithData: [self decodeBase64WithString:[registerDataDict objectForKey:#"imageOne"]]];
You need to import Base64.h
You can directly use like this way:
UIImage *image = _btnaddCountryIcon.imageView.image;
NSData *imageData = UIImagePNGRepresentation(image);
NSString *base64 = [Base64 encode:imageData];
directly you can convert to NSString. This code works fine for me.
my NSData object contains within byte of a photo.
I would like to convert this object to a UIImage.
My problem is that he could not give a name all'oogetto UIImage. example myphoto.jpg
this is my code:
NSString * bytestring =(NSString *)[bean image];
//contain byte of my photo
NSData * byte = [bytestring dataUsingEncoding:NSUTF8StringEncoding];
UIImage *image=[[UIImage alloc]initWithData:byte];
UIButton *bt=[[UIButton alloc]initWithFrame:CGRectMake(80,80,100,100)];
[bt setImage:image forState:UIControlStateNormal];
[self.boxView addSubview:bt];
the button does not display my photos.
how can I do?
Since it appears that [bean image] is actually a base 64 encoding of the image's data, you need to use a method to convert the encoded data back into unencoded data.
There is no standard method for this but there are plenty you can find and use. Your code then becomes something like this:
NSString *byteString = [bean image]; // no need for a cast if it's already an NSString
NSData *byte = // call some method to convert base 64 encoded string into an NSData object
Have a look at:
convert base64 decoded NSData to NSString
I have been using NSJSONSerialization class for converting fields of my object to JSON. Sadly only NSString, NSNumber, NSArray, NSDictionary, or NSNull types are supported.
As my object has one additional field, that is UIImage, I am at loss as to how to deal with it. I am sure many people have encountered this common problem, so what is best method to approach this?
You can encode UIImage data by base64, and add it to json object.
To get data from UIImage, you can use UIImagePNGRepresentation and UIImageJPEGRepresentation.
The code like this,
NSData *imageData = UIImagePNGRepresentation(image);
NSString *base64encodedStr = base64encode(imageData);
[dict setObject:base64encodedStr forKey:#"myImage"];
//then covert dict to json object.
To restore UIImage data, just parse json object and decode the data by base64.
Hope this can help you.
You could convert your images data to a string and then write that string.
NSData *imageData = UIPNGRepresentation(image);
NSString *imageString = [[NSString alloc] initWithData:imageData encoding:NSUTF8StringEncoding];
//I don't know how to use NSJSONSerialization
//[NSJSONSerialization serializeString:imageString];
NSString *base64encodedStr = [imageData base64Encoding];