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.
Related
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.
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.
I am fairly new to archiving an object I have. I have a dictionary that I want to archive.. so first I got the path by doing:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
dictionaryPath = [paths objectAtIndex:0];
so is the next step to append this dictionaryPath with a .txt? Or should I create something first at this directory? What is then the data type?
I want to be able to use it for:
[NSKeyedArchiver archiveRootObject:locationDic toFile:dictionaryPath];
Where dictionaryPath is the path to store this NSDictionary
NSSearchPathForDirectoriesInDomains will give you an array of directory paths. So if you take the first one, you need to append a filename (whatever filename you'd like, with whatever extension you'd like), and then use that path as the location to write out to.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dictionaryFilePath = [documentsDirectory stringByAppendingPathComponent:#"Filename.extension"];
[NSKeyedArchiver archiveRootObject:locationDic toFile:dictionaryFilePath];
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.
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];