if i place a image.png file in my resource, iphone will be able to read it. if i place a image.png file in iphone document folder it doesn't read?
i am sending the image over from my server, into the document folder. no problem with that.
i thought iphone will auto find the image file either in resource folder or document folder?
i did write any code for my app to locate the folder of my image, just called it by its file name, which is correct.
any ideas?
thks
// Get path to doc folder
NSArray *docpaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [docpaths objectAtIndex:0];
NSString *docpath = [documentsDirectory stringByAppendingPathComponent:#"Data.plist"];
self.data = [NSArray arrayWithContentsOfFile:docpath];
done loading the plist from doc folder.
now i go to plist, get the file name and display for cell.
NSDictionary *dataItem = [data objectAtIndex:indexPath.row];
cell.icon = [UIImage imageNamed:[dataItem objectForKey:#"Icon"]];
how can i specifiy the image is in my document folder?
You can create an UIImage from a file by using imageWithContentsOfFile. However you can not read the file just specifying the name if it is in document folder. You need to get the path of your file. Check this answer to get an idea of how to get the file path of document folder.
-- edit
If your plist contains the file names in document folder, then append the filenames from plist to document folder path to get the full path. Hope it helps.
NSArray *docpaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [docpaths objectAtIndex:0];
NSDictionary *dataItem = [data objectAtIndex:indexPath.row];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:[dataItem objectForKey:#"Icon"]];
cell.icon = [UIImage imageWithContentsOfFile:imagePath];
Related
I don't know how can I create XML document using GDataXML nor I could find any good link which can help me.Please tell me how can I perform this or give some good link.
Take look at http://www.raywenderlich.com/725/how-to-read-and-write-xml-documents-with-gdataxml tutorial. It will help you.
First create and store the xml value to a string.
Then use the following method to create the xml file in the document directory.
//Method writes a string to a xml file
-(void) writeToXMLFile:(NSString *)content
{
//get the documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the documents directory:
NSString *fileName = [NSString stringWithFormat:#"%#/xmlfile.xml",documentsDirectory];
//save content to the documents directory
[content writeToFile:fileName atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];
}
I have an image named my-image. Can I save this image to the application bundle? If yes, can you provide me the code or paths.
As of now, I have the following code –
NSData * imageData = UIImagePNGRepresentation(userSavedImage); //convert image into .png format.
NSFileManager * fileManager = [NSFileManager defaultManager];//create instance of NSFileManager
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it
NSString * documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory
NSString * fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.png",txtImageName.text]]; //add our image to the path
[fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the image
NSLog(#"image saved");
However it is saving in
/Users/tsplmac01/Library/Application Support/iPhone Simulator/4.3/Applications/BC5DE1D8-B875-44BC-AC74-04A17329F29A/.
& not in the application bundle. Please tell me how I can do it.
The app bundle is read only for the app, so it is not possible to write to it.
This is a good restriction, because it would otherwise be wiped out when the app gets updated. You may want to write to the documents directory instead.
I am using dropbox API and downloading file in application document folder.
I am able to view list of files in table from document directory. How can I read photos or file from this table view?
And what about the iPhone. Can we access document folder directly?
This is the more commonly used approach to get the documents directory –
NSArray *searchResults = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [searchResults objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
You can use NSFileManager to examine the contents of the directory. You can look at the contentsOfDirectoryAtPath:error: method.
Access document Folder -
NSDictionary *theCatalogInfo=nil;
NSString *theCatalogFilePath = [NSString stringWithFormat:#"%#/Documents/",NSHomeDirectory()];
theCatalogFilePath = [theCatalogFilePath stringByAppendingString:#"File Name"];
if(nil!=theCatalogFilePath)
{
theCatalogInfo=[[NSDictionary alloc]initWithContentsOfFile:theCatalogFilePath];
}
return theCatalogInfo;
How would i download and save an image to the root of the application so basically i can access the image via
[UIImage imageNamed:#"myimage.jpg"];
Thanks
Mason
First you need to get the Image Data
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://media03.linkedin.com/mpr/mpr/shrink_80_80/p/3/000/064/2e2/1bd3849.jpg"]];
Then you need to write the data to Documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pathLD = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"imageLD%d.jpeg",[[NSDate date] timeIntervalSince1970]]];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: pathLD]){
[imageData writeToFile:pathLD atomically:YES];
} else {
NSLog(#"File exists at path:%#", pathLD);
}
To get the image from Documents you do:
[UIImage imageWithContentsOfFile:pathLD];
Good luck :D;
You shouldn't for various reasons. Consider your application directory as being read-only.
You should use the Documents or Library directory. Apple recommends that you should use the Documents directory if your files should be user-accessible via iTunes (if you have enabled the file sharing via iTunes), or use a custom subdirectory of Library (which you need to create with NSFileManager) if it shouldn't be user-visible (but should be backed up).
You can query the path to the Library directory like this:
NSArray *paths;
paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
NSUserDomainMask, YES);
// dir = [paths objectAtIndex:0];
Substitute NSLibraryDirectory with NSDocumentsDirectory to get the Documents directory.
Then you would make a method that returns the full path to your image, and you would then do:
[UIImage imageWithContentsOfFile:[self pathForImage:#"myimage.jpg"]]
I retrieve an image I have saved at the documents directory like this:
//set background image
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:#"/myFolder/back.png"];
backgroundImage.image =[UIImage imageWithContentsOfFile:imagePath];
But it is not shown :(
What am I doing wrong?
PD:Yes, it has been saved with the same path and filename (no upper/lower case differences) and yes, I tried opening the image at that path and it works.
Thanks in advance!
It was being done in the wrong place. I was doing it at init instead of viewDidLoad.