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.
Related
As i am saving a image which has transparent part but while saving it in Document folder, the transparent part changed to white.
Below is the code ....
NSData *dataPhoto = UIImageJPEGRepresentation(imageBigPhoto, 1.0);
//NSData *dataPhoto = UIImagePNGRepresentation(imageBigPhoto);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPathWithFoldername=[documentsDirectory stringByAppendingFormat:#"/%#",[[NSUserDefaults standardUserDefaults]valueForKey:#"PhotoFolderName"]];
//NSLog(#"%#",fullPathWithFoldername);
NSString *fullPath = [fullPathWithFoldername stringByAppendingPathComponent:name];
[dataPhoto writeToFile:fullPath atomically:YES];
JPEG images don't support transparency. PNG images do.
I think you have commented your PNG Representation of Image, JPEG Representation does not save transparency. Just Enable you commented code and try.
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"]]
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.
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.
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];