Images not loading from sub directories of document directory - iphone

HI,
I am loading images from sub directory of document directory. Here is the code.
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
cell.imageView.image = [UIImage imageWithContentsOfFile:[documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:#"images/category/%#", element.image]]];
My images resides at DocumentDir/images/category/img1.png, DocumentDir/images/category/img2.png, DocumentDir/images/category/img3.png .....
However when images are at DocumentDir it loads fine. But when it is in sub directories like above it does not load. Am I doing wrong here?
Thank you

Solved the issue. There was a new line character at the end of the "element.image" variable.

NSString *docPath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:#"images/category/%#", element.image]];
cell.imageView.image = [UIImage imageWithContentsOfFile: docPath];
use this, One more thing image file should contain the .png or .jpg. It is required when you use the imageWithContentsOfFile method.

Related

file exist in Document folder but not for Xcode

I have a strange problem, this code check if a file exist inside document folder:
- (BOOL) checkIfFileExist:(NSString *)path {
NSArray *documentsPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [documentsPaths objectAtIndex:0];
NSString *fileDaControllere = [documentsDirectory stringByAppendingString:path];
if ([[NSFileManager defaultManager] fileExistsAtPath:fileDaControllere]) {
NSLog(#"exist");
return YES;
}
else {
NSLog(#"not exist");
return NO;
}
}
the problem is that I get alway file not exist while the file exist (in this case the path is Style.css)! where is the mistake?
The path seems to be correct:
Path: /Users/kikko/Library/Application Support/iPhone Simulator/6.0/Applications/38161AFA-2740-4BE2-9EC4-C5C6B317D270/Documents/Style.css
Here you can see the path on xcode and real path
http://www.allmyapp.net/wp-content/iFormulario/1.png
http://www.allmyapp.net/wp-content/iFormulario/2.png
The problem may be in the fact that you just append the file without with check if there is a path delimitor:
NSString *fileDaControllere = [documentsDirectory stringByAppendingString:path];
Thus you would become something like ../DocumentsStyle.css but it should be ../Documents/Style.css.
NSString has a special method for appending path components stringByAppendingPathComponent:
NSString *fileDaControllere = [documentsDirectory stringByAppendingPathComponent:path];
At the end I solved this issue, the strange think is that I don't know how I solved it,
the first problem is that I pass to checkIfFifFileExist the absolute path while I need to pass it relative path, and the the function trasform it to absolute path (my big big error), after this I think the problem is the use of "/", I delete all and rewrite all the code and I doing some test.
I copy folder from bundle:
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *folderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"/icone"];
NSString *iconePath = [documentsDir stringByAppendingPathComponent:#"/icone"];
[[NSFileManager defaultManager] copyItemAtPath:folderPath toPath:iconePath error:nil];
Then I make the path of my image in this way:
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *imagePath = [documentsDir stringByAppendingPathComponent:self.objMateria.iconaMateria];
and now the file exist, a strange thing is that if:
self.objMateria.iconaMateria = /icona/Home/fisica.png
or
self.objMateria.iconaMateria = icona/Home/fisica.png
nothing change, I see the image, while I think that one of this has a wrong path...

How to locate the correct image path

I save my images in the folder "pic" (an example of a full path for an image: /Users/apple/Library/Application Support/iPhone Simulator/5.1/Applications/2526EA68-229D-40BC-BB57-56E46EB55691/Documents/pic/1.jpg).
The "Document" path is got by calling:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
So the path is definitely correct . But when I do:
[tempImage setImage:[UIImage imageNamed:fullImagePath]]
It seems that the image is not found, because the image is not shown. I want to know the reason for this problem and how to fix it?
imageNamed only works for images in the main bundle, not in subdirectories.
Use
[UIImage imageWithContentsOfFile:filePath]
instead.

download and save image to root

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

Show image from documents folder

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.

reading image from document folder

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