dataWithContentsOfFile:filepath of NSData sometimes works, sometimes has no function - iphone

I used the codes below to load image file in app bundle.
The codes work! imageData does NOT return 0x0
NSMutableString *sss;
sss=[[NSMutableString alloc] initWithString: [[NSBundle mainBundle] resourcePath]];
[sss appendString:#"/"] ;
[sss appendString:#"thumbnail.png"];
NSData *imageData = [NSData dataWithContentsOfFile:sss];
but the codes below in which almost everything is same have no function, imageData always DOES return 0x0, it looks like [NSData dataWithContentsOfFile:filepath ] does not work for large size image file
NSMutableString *sss;
sss=[[NSMutableString alloc] initWithString: [[NSBundle mainBundle] resourcePath]];
[sss appendString:#"/"] ;
[sss appendString:#"original.png"];
NSData *imageData = [NSData dataWithContentsOfFile:sss];
Both of thumbnail.png and original.png are in the same directory of main bundle.
thumbnail.png
original.png
Welcome any comment
Thanks
Marc

It won't have anything to do with the size of the file. Check and see if you've spelled the file name correctly in your code. The file names are case-sensitive, so if the file is named original.PNG, then you'll need to make sure it is written that way in code as well.
Make sure that the original.png file has been added to your project, and if so, try removing it from your project and re-adding it. When you drag the file into your project, make sure the option "copy files..." is checked.

check out if it really contains on resource .
check that file is in:
Targets/ProductName/Copy Bundle Resources

I also think that the file has not been added to resources. Check the 'Resources' folder in your xcode project, and if it isnt there, put it in there. Either by dragging it in there, or by rightclicking -> new file.

Related

How to convert sound file to binary file and send in http post?

I recorded sound in file called "recordedTmpFile".now i want to send the file to server .
i tried this code
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"recordedTmpFile" ofType:#"caf"];
NSData *postData = [filePath dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
//nsdata to string
NSString *content = [[NSString alloc] initWithBytes:[postData bytes]
length:[postData length] encoding: NSUTF8StringEncoding];
am getting null value in "content"
please help me to fix this problem
Did your app record the sound and write it into the file? Remember that the app bundle is read-only, so if you tried to write the file to a location in the main bundle, as your code seems to indicate, then the write operation probably failed. If that's the case, then of course there's no file to read or send.
Try changing the path to which you record data to someplace in the documents directory, which is writeable.

can i update my local text file on iphone

i have a text file so can i update this text file from web ???
lets say i have
1. NSString *filePath = [[NSBundle mainBundle] pathForResource:#"MyFile" ofType:#"txt"];
2. NSData *myData = [NSData dataWithContentsOfFile:filePath];
3. if (myData) {
4. // do something useful
5. }
right now Myfile.text is having 10 data so how to insert 10 more to this data??
plz help
Files stored inside your app bundle will not be writable.
You will have to make a copy of the file and store it in the Documents or Library folder before you will be able to edit it.

Simplest way on iPhone to unzip downloaded file?

Goal: download a zipped file, unzip it, and save it in the iPhone app's Documents directory.
The following code makes use of the initWithGzippedData method that was added to NSData in the Molecule app found here:
http://www.sunsetlakesoftware.com/molecules
As adapted to my app:
NSString *sFolder = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *sFileName = [sFolder stringByAppendingPathComponent:#"MyFile.db"];
NSURL *oURL = [NSURL URLWithString: #"http://www.isystant.com/Files/MyFile.zip"];
NSData *oZipData = [NSData dataWithContentsOfURL: oURL];
NSData *oData = [[NSData alloc] initWithGzippedData:oZipData];
[oZipData release];
b = [oData writeToFile:sFileName atomically:NO];
NSLog(#"Unzip %i", b);
Result: A zip file is successfully downloaded. From it a new, supposedly unzipped file is created in the Documents directory with the desired name (MyFile.db) but it has zero bytes.
Anybody see the problem? Or else is there a simpler way to unzip a downloaded file than the one used in the Molecules app?
I think that your problem may be that you are attempting to gzip-deflate a Zip file. Those are two different compression algorithms.
I based the gzip-deflating code in Molecules on this NSData category (the code of which I've copied into this answer) provided by the contributors to the CocoaDev wiki. What you'll want to do is use their -zlibDeflate implementation, which should properly unzip a Zip file.
Unrelated to your problem, instead of using NSHomeDirectory() and appending a path component, the recommended approach for finding the documents directory is the following:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
You should make sure your file is never too big, as you are loading it fully into memory before the unzip starts.

read single line from text file in objective-C

i'm new to iPhone programming and coding in XCode SDK.I want to access and read the configuration file which i have placed in Resource folder in XCode,my configuration file looks like this
#key=value$
#vinu=flower$
#cathy=fruit$
I want to compare the key and access the value from configuration file.
Since i'm working with iPhone OS, i cant use NSWorkspace.Hence i'm using NSFileHandle.
This is how my code looks like,
NSString *path = [[NSBundle mainBundle] pathForResource:#"configuration" ofType:#"txt"];
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];
NSString *txtString = [[NSString alloc] initWithData:
[readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
please let me know is the procedure correct, and how to proceed. ??
Thank You.
Do yourself a favor and save it as a plist, and not a straight text file.
However, if you need to read it in like that, the simplest way is to read it into a string and then go from there, ie:
NSString * fileContents = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
If the file is super large and the only reasonable way to read it is line-by-line, then you can quickly see that this is something that has come up before (Particularly: Objective-C: Reading a file line by line).

Loading data files in iPhone project

How does one read a data file in an iPhone project? For example, lets say I have a static file called "level.dat" that is structured as follows:
obstacles: 10
time: 100
obstacle1: 10,20
...
I would like to read the contents of the file into a NSString then do the parsing. How do I read the contents of a file into a string? Also, where in the project should the "level.dat" file reside? Should it be under "Resources" or just in the main directory?
Thanks in advance!
See this answer: How to fopen() on the iPhone? which shows how to get access to resources in your bundle. Once you have the path, just use [NSString stringWithContentsOfFile:encoding:error:].
NSString *path = [[NSBundle mainBundle] pathForResource: #"level" ofType: #"dat"]
NSError *error = nil;
NSString *data = [NSString stringWithContentsOfFile: path
encoding: NSUTF8StringEncoding
error: &error];
While this isn't what you asked for, consider turning your files into plists. You will have to reformat them into XML, but then you can load them straight into a NSDictionary with:
dict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"levels" ofType:#"plist"]];
Have you considered putting the data in an SQLite database instead of a flat file? I find that the API is very easy to use on the iPhone.
It is how I do all of my data storage on the phone now.
If you need help parsing the data string, there's a helpful article on Cocoa For Scientist