Reputation: 75
I would like to save image in a folder. I already the code. to download and save image but I would like to know how to find the path of ma folder ! I create a directory in xcode but the images don't find it and there aren't image in it. So, I would like to save images in "img_bkgrd" folder.
[UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", imageName]] options:NSAtomicWrite error:nil];
to use the image I use this method:
-(UIImage *) loadImage:(NSString *)fileName inDirectory:(NSString *)directoryPath{
UIImage * result = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", directoryPath, fileName]];
return result;
}
Can you send me the correct path ? thanks
Upvotes: 0
Views: 45
Reputation: 21808
You can't save the images there. First of all, this is not actually a folder. It is called a group. It is a part of application bundle, which is readonly. You need to save data into Documents
directory. Something like that:
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *imageFolderPath = [documentsDirectory stringByAppendingPathComponent:@"/img_bckr"];
if (![[NSFileManager defaultManager] fileExistsAtPath:imageFolderPath])
[[NSFileManager defaultManager] createDirectoryAtPath:imageFolderPath withIntermediateDirectories:NO attributes:nil error:&error];
NSString* imagePath = [imageFolderPath stringByAppendingPathComponent:@"MyImage"];
[UIImageJPEGRepresentation(image, 1.0) writeToFile:imagePath options:NSAtomicWrite error:nil];
You may store imagePath
somewhere or build it later based upon file name and then simply:
UIImage* img = [UIImage imageWithContentsOfFile:imagePath];
Upvotes: 1