Re: Newbie: Encoding NSData to a string
Re: Newbie: Encoding NSData to a string
- Subject: Re: Newbie: Encoding NSData to a string
- From: Nir Soffer <email@hidden>
- Date: Tue, 27 Mar 2007 02:44:07 +0200
On Mar 26, 2007, at 07:05, Joao Lourenco wrote:
Hello
I'm starting to learn Cocoa and would like to do the following
Read an image from a file
Encode the image as an "ascii" string
Send the image as an ASCII string in the body of a post message
This is the code I have so far:
Code:
NSImage *pictureToSend = [[NSImage alloc] initWithContentsOfFile:
filePath];
NSData *dataEncodedPicture = [pictureToSend TIFFRepresentation];
// do whatever you need to do to create and/or modify your NSImage
NSBitmapImageRep *imageRep = [NSBitmapImageRep
imageRepWithData:dataEncodedPicture];
NSDictionary *imageProps = [NSDictionary dictionaryWithObject:
[NSNumber numberWithFloat:0.9] forKey:NSImageCompressionFactor];
dataEncodedPicture = [imageRep
representationUsingType:NSJPEGFileType properties:imageProps];
You want to read a file in some format, and create JPEG format. I
don't understand why you want to create a tiff format in the middle.
You can use code like this:
NSData *imageData = [NSData dataWithContentsOfFile:filePath];
NSBitmapImageRep *imageRep = [NSBitmapImageRep
imageRepWithData:imageData];
Then get your jpeg data as you did.
NSString* stringEncodedPicture = [[NSString alloc] initWithData:
dataEncodedPicture encoding: NSUTF8StringEncoding];
NSLog(stringEncodedPicture);
All is well until: stringEncodedPicture = [[NSString alloc]
initWithData: dataEncodedPicture encoding: NSUTF8StringEncoding];
You need to encode the data in base64 or similar format. The easiest
method is be to create a plist of the image data:
NSData *plist = [NSPropertyListSerialization dataFromPropertyList:
dataEncodedPicture
format: NSPropertyListXMLFormat_v1_0
errorDescription:Null];
You can send the plist data as is in your post body. If the machine
procssing the request can read plists, your are done :-)
Best Regards,
Nir Soffer
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden