Unable to get files from temp folder - iphone

I'm creating picture browser app similar to Photo app on iPhone. I download pictures from web storing them on TEMP folder
NSString *uniquePath = [NSTemporaryDirectory() stringByAppendingPathComponent: filename];
[UIImageJPEGRepresentation(localImage, 100) writeToFile:uniquePath atomically:YES];
and later retrieving to display them on UIImage View.
if([[NSFileManager defaultManager] fileExistsAtPath: uniquePath])
{
UIImage* localImage = [UIImage imageWithContentsOfFile: uniquePath];
}
Everything work well until I manually delete the items in folder
[[NSFileManager defaultManager] removeItemAtPath:NSTemporaryDirectory() error:&errorInfo];
Once above code is executed, I'm no longer able to store or retrieve images, but control flow shows that images exist in temp folder...
Can anyone identify what would be the problem ?
PS: Images are ~25Kb in size

[[NSFileManager defaultManager] removeItemAtPath:NSTemporaryDirectory() error:&errorInfo];
You are removing the temp directory instead of temp files. NSTemporaryDirectory does NOT create directory if it doesn't exist.

Related

How to get the files inside a folder in Supporting Files in xcode 4.2?

I have done the following steps.
1. Created an xcode project and created a folder named "Images" inside Supporting Files.
Dragged and dropped 4 images into it. Tried to access those files using the following code
NSString *pathString = [[NSBundle mainBundle] pathForResource:#"Images" ofType:nil];
NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:pathString error: nil];
NSLog(#"the fileList is %d",[fileList count]);
The count is always 0. What should I do now? There are files inside it and I am able to use it in imageviews.
So what is the mistake that I am making?
The Xcode does not generate any folders in your app bundle that corresponds to groups. Any resources added are present in Copy Bundle Resources in your target's Build Phases. You then access your resource under [[NSBundle mainBundle] bundlePath] path.
But if you want to count files under any folder then while adding that folder you must check the option :
"Create folder references for any added folders."
This will create folders and sub-folders in the same hierarchy as you add them.Then you can easily count them in the same manner you are doing above...
Otherwise the app bundle has all of your resources at one place not in any folder as you say "Images".Use following code :
NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[[NSBundle mainBundle] resourcePath] error: nil];
NSLog(#"the fileList is %d",[fileList count]);
It will list all your resources.
Try this,
NSString *imagespath = [[NSBundle mainBundle] pathForResource:"yourimagename" ofType:#"png" inDirectory:#"images"];

iOS File won't write to directory

I'm using cocoaHTTPServer library that allows you to store files "publicly". For some reason I can't write files to the directory.
I can write to my local file directory, but not the public "Web" folder.
/var/mobile/Applications/42CE308A-F804-47E3-BA07-CB921B909FFB/Documents/song.caf
/var/mobile/Applications/42CE308A-F804-47E3-BA07-CB921B909FFB/MyApp.app/Web/song.caf
The top one works, the bottom one doesn't.
Meaning, when I save the file, then check for the file in the documents directory, it is there, when I save, and then check the "Web" directory, the directory is empty.
And i'm using AVAssetWriter to save the song.
NSString *exportPath = [DataCenter sharedDataCenter].songURLLocal;
if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath]) {
[[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];
}
NSURL *exportURL = [NSURL fileURLWithPath:exportPath];
AVAssetWriter *assetWriter = [AVAssetWriter assetWriterWithURL:exportURL
fileType:AVFileTypeCoreAudioFormat
error:&assetError];
What does the assetError output through NSLog?
Also, You will not be able to write the the Web directory which is clearly located in your app bundle — which is sandboxed. The only directory you can write to is the Documents directory.
Also, what is the NSString *exportPath = [DataCenter sharedDataCenter].songURLLocal?
If exportPath is the Web directory then your methods will always fail.

How to download file and save in local iphone application?

I have locally saved one xml file and showing the details by using NSXmlParser(Which is stored in Resource folder). But, now i want to download new xml from url(from client side) and need to store that new xml file in local and also need to delete existing one from the application. How can i do this? Any suggestions? There is any sample code/project for reference? Please help me. Thanks for reading my poor english. Thanks is advance.
although u can delete file from yr resource directory and save new one but the my way is to perform file operation mostly on app diretories like document directory or cache directory.
first u will to save yr xml file from app resource to cache directory then access file from there.And u can also remove file from resource directory ,its no longer needed.
any new file available in internet then first remove old one from cache and add new one to cache directory.
whole thing is as follows:-
//get your cachedirectory path
NSArray *imagepaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachdirectory = [[NSString alloc] initWithString:[imagepaths objectAtIndex:0]];
//first save your file from resource directory to app's cache directory
NSString * path = [[NSBundle mainBundle] pathForResource:#"fileinresource" ofType:#"xml"];
NSData *filedata = [NSData dataWithContentsOfFile:path];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *savename = [cachdirectory stringByAppendingPathComponent:#"mynewfile.xml"];
[fileManager createFileAtPath:savename contents:filedata attributes:nil];
//remove file from resource directory
[fileManager removeItemAtPath:path error:nil];
//new file from internet is avilable then do following:first remove old file from cache
[fileManager removeItemAtPath:cachdirectory error:nil];
//save new file from internet
NSData *newfiledata=[NSData dataWithContentsOfURL:[NSURL URLWithString:myxmlurlstringpath]];
[fileManager createFileAtPath:savename contents:newfiledata attributes:nil];

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.

iphone storing and then reading a file from Documents folder

this must be easy but I want to put a file in the Documents folder which is read in at start up. I have the code on how to read and have confirmed its looking in the correct directory. However the file I have ,RootList.txt when saved in the Resources folder in xcode, is stored under the Root.app folder and the Documents folder is empty. Therefore its not finding this file when I start the app.
Is there a way to ensure a file is built into the Documents directory at start up (I'm running this in the simulator).
The alternative is a plist which works fine as well but I'm just curious.
In these situations I follow this approach:
First save your RootList.txt in Resources folder in xCode.You have nothing in your Documents folder, yet.
In the beginning of your applicationDidLaunch call, do:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [docsDirectory stringByAppendingPathComponent:#"RootList.txt"];
if(![fileManager fileExistsAtPath:path])
{
NSData *data = [NSData dataWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingString:#"/RootList.txt"]];
[data writeToFile:path atomically:YES];
}
Now, your file is in Documents folder at startup.

Categories