I need a method to convert a UIImage in a NSString and then convert the NSString back to a UIImage.
image name? path? url? image data as base64? What do you try to do?
Convert it to a binary stream instead (NSData
). This will depend on the format of your UIImage
. If it's a JPEG/PNG for instance, you do:
NSData*data1 =UIImageJPEGRepresentation(image,1.0);
NSData*data2 =UIImagePNGRepresentation(image);
UPDATE: Converting the binary data to NSString
is a bad idea, that is why we have the classNSData
. The OP wants to be able to send it as a data stream and then reconstruct it again;NSString
will not be needed for this.
I got a valid NSString using [UIImagePNGRepresentation(image) base64Encoding]. Now, all I need is a way to convert a Base 64 encoded NSString in a NSData.
All you need is in NSString: -dataUsingEncoding:
and -initWithData:encoding:
Convert to PNG or JPEG using UIImagePNGRepresentation or UIImageJPEGRepresentation, which will return an NSData, and then convert the NSData to a string (not sure how you want to do that mapping). How about just dealing with the NSData? You can read/write that to a file.
I don't want to write to the disk because the I/O latency is too high.
Create an UIImage from NSString
my problem is: i get from webservices an image. this is stored in NSString. and now i want show it as UIImage. But I dont know how to do it.
how i can convert data from NSString in NSData?
you can use the below methods for converting string to data
[NSData dataFromBase64String:yourString]; [NSData dataFromBase64EncodedString:yourString]
How to Create UIImage from NSData and Avatar Data of XMPP?
This question is related to Iphone SDK, NSData and UIImage.
I am trying to create an image from the Avatar Data returned from the xmpp like the following:
<presencefrom='yyy@184.73.164.51/spark'to='ken@184.73.164.51/424978324712783686768453'id='Oj02v-45'><status>Away due to idle.</status><priority>0</priority><show>away</show><xxmlns='vcard-temp:x:update'><photo>a3f549fa9705e7ead2905de0b6a804227ecdd404</photo></x><xxmlns='jabber:x:avatar'><hash>a3f549fa9705e7ead2905de0b6a804227ecdd404</hash></x></presence>
So in this case, I assume that a3f549fa9705e7ead2905de0b6a804227ecdd404 is the photo data. So How can I transfer this into NSData?
I think if I can get the NSData object, I can easily create the UIImage, right?
I think "a3f549fa9705e7ead2905de0b6a804227ecdd404" is the photo data this is my codes:
NSString* command =@"a3f549fa9705e7ead2905de0b6a804227ecdd404";
command =[command stringByReplacingOccurrencesOfString:@" " withString:@""];
NSMutableData*commandToSend=[[NSMutableData alloc] init];
unsignedchar whole_byte;
char byte_chars[3]={'\0','\0','\0'};
int i;
for(i=0; i <[command length]/2; i++){
byte_chars[0]=[command characterAtIndex:i*2];
byte_chars[1]=[command characterAtIndex:i*2+1];
whole_byte = strtol(byte_chars, NULL,16);
[commandToSend appendBytes:&whole_byte length:1];
}
UIImage*image =[UIImage imageWithData: commandToSend];
However, it doesn't work. Anyone knows what's wrong with it?
In XMPPPresence.m add this method
-
(NSString*)photo {
NSXMLElement*xElement =[self elementForName:@"x" xmlns:@"vcard-
temp:x:update"];
NSString*photoHash =[[xElement elementForName:@"photo"]
stringValue];
return photoHash;
}
// In XMPPStream's delegate:
-(void)xmppStream:(XMPPStream*)stream didReceivePresence:
(XMPPPresence*)presence {
NSString*photoHash =[presence photo];
if([photoHash length]>0){ // in case when there's no photo hash
XMPPJID *rosterJID =[presence from];
BOOL requestPhoto =...// determine if you need to request new
photo or nor
if(requestPhoto){
NSXMLElement*iqAvatar =[NSXMLElement elementWithName:@"iq"];
NSXMLElement*queryAvatar =[NSXMLElement elementWithName:@"vCard"
xmlns:@"vcard-temp"];
[iqAvatar addAttributeWithName:@"type" stringValue:@"get"];
[iqAvatar addAttributeWithName:@"to" stringValue:[rosterJID full]];
[iqAvatar addChild:queryAvatar];
XMPPIQ *avatarRequestIQ =[XMPPIQ iqFromElement:iqAvatar];
[stream sendElement:avatarRequestIQ];
}
}
}
// And when buddy will send photo, it will be in vcard BASE64-encoded. // You will receive it as IQ:
-(BOOL)xmppStream:(XMPPStream*)stream didReceiveIQ:(XMPPIQ *)iq {
XMPPElement*vCardPhotoElement =(XMPPElement*)[[iq
elementForName:@"vCard"] elementForName:@"PHOTO"];
if(vCardPhotoElement !=nil){
// avatar data
NSString*base64DataString =[[vCardPhotoElement
elementForName:@"BINVAL"] stringValue];
NSData*imageData =[NSData
dataFromBase64String:base64DataString]; // you need to get NSData
BASE64 category
UIImage*avatarImage =[UIImage imageWithData:imageData];
XMPPJID *senderJID =[iq from];
[self xmppStream:stream didReceiveImage:avatarImage
forBuddy:senderJID]; // this is my custom delegate method where I
save new avatar to cache
}
return NO;
}
Hope this will help you.
That is the picture hash you now have to send a vcard request which will contain the same hash for verification and binval containing the picture data in base64