How can I save NSData to my database - iphone

I have a object of UILabel
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
label1.text = #"LABEL AREA";
I want to save it in my databse(sqlite).
My idea is to convert this object to a NSData using
[NSKeyedArchiver archivedDataWithRootObject:label1]; //step1
and convert this NSData to a NSString(step2), then save that NSString to the database(step3).
When I need my UILabel. I can get the NSString from database, convert it to NSData, and use
[NSKeyedUnarchiver unarchiveObjectWithData:];
to get my UILabel.
But I have a problem in "step 2". When I use
NSString *strWithEncode = [[NSString alloc] initWithData:dataRaw encoding:NSUTF8StringEncoding];
I get a null object. I don't know the reason.
Why?
Thanks a lot!

You could either a) Store the data as a BLOB or b) Store the text of the string instead of the actual string. B seems like a better choice because you will be storing less data, and the contents of the DB will be human readable, should you need that information for debugging later on.

Because the NSData represents an encoded UILabel and not a UTF-8 encoded NSString, trying to initialize a UTF-8 string from the data will almost certainly not work. If you absolutely have to store the data as a string, try using some form of data-to-string encoding. Try using base32 or base64.

Related

How to convert UIImage to JSON file in iPhone?

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];

How i can use stringWithContentOfFile while text is not UTF8 format

I have many files html type. Now i want to get content of them. But the text is not UTF8 format so stringWithContentOfFile function return nil. The problem is i can't convert text of file to UTF8 because there are many files. I tried use WebView but not success. There are any way to read files?
Get the data using dataWithContentsOfFile: method and then convert it into an NSString object using initWithData:encoding: method. You can provide the encoding there.
NSData * data = [NSData dataWithContentsOfFile:filePath];
NSString * fileInString = [[NSString alloc] initWithData:data encoding:yourEncoding];

how to convert NSString to NSMutableArray?

i have an NSString that is value in plist format. i download this string from url. but i dont wanna write it to a file. when the string comes Asynchronous, i want to put it to nsmutablearray.
how can i convert string (in plist format) to nsmutablearray?
there is some methods, initWithContentsOfURL, initWithContentsOfFile. but no intiwithstring.
this method works Synchronous:
NSMutableArray *tmpArray = [[NSMutableArray alloc] initWithContentsOfURL:url];
There is a -propertyList method in NSString.
NSMutableArray* tmpArray = [[theString propertyList] mutableCopy];
...
[tmpArray release];
Note that this method will throw an exception (i.e. throw) if the string is not in plist format. To have a better error checking, try to download the data as NSData, and use the NSPropertyListSerialization methods.

iphone scanning a dat file for data

I am trying to remake a program I have made in C# in OBJ-C.In C# I used streamreader to search the data file for the line I am looking for then convert that line into a string that I can work with.
I have looked at NSScanner but I'm not sure if thats quite waht I'm looking for but I'm by no means a cocoa expert.
All I would like to be able to do is have it search a data file for an occurance of a string, then when/if it finds an occurance of that string, it returns the line that string was found on as a string.
Any ideas?
If your data file isn't to large to fit in memory, you can just load it into a string and search it using string methods. For example:
NSData *data = [NSData dataWithContentsOfFile:#"/path/to/file.dat"];
NSString *dataString = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
for (NSString *line in [dataString componentsSeparatedByString:#"\n"])
if (!NSEqualRanges([line rangeOfString:searchString], NSMakeRange(NSNotFound,0)))
return line;

UIImage from bytes held in NSString

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.