I am getting nil image when i try to set image from local file path (Caches directory)
file:///......
NSString* localImagePath=#"file:///Users/mac/Library/Application%20Support/iPhone%20Simulator/7.1/Applications/BD2135B6-5E03-4797-960E-B6C2BF2D6958/Library/Caches/myImage.jpeg"
[[UIImage alloc]initWithContentsOfFile:localImagePath];
I searched through many questions but not able to find the right way to do it.
- (IBAction)saveImage {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:#"savedImage.png"];
UIImage *image = imageView.image; // imageView is my image from camera
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];
}
May be the issue was of url encoding so I solved this by getting the caches directory path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [paths objectAtIndex:0];
NSString *localImagePath = [cacheDirectory stringByAppendingPathComponent:#"myImage.jpeg"];
[[UIImage alloc]initWithContentsOfFile:localImagePath];
Related
Here is the deal... I am creating an app (from another one of my apps) but I am altering to from using only the NSDocumentDirectory, which obviously allows the user to see all of the user files, to seeing only a few of the files... namely user created PDFs.
I have it working... but, nothing shows in the FileSharing/Documents window in iTunes.
First are the methods that invoke the NSApplicationSupportDirectory in the persistence model...
+(NSString *)getDocumentpath
{
//NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
return [paths objectAtIndex:0];
}
+(NSString *) documentsDirectoryPath
{
//NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return documentsDirectory;
}
+(void) copyResourceFileToDocumentsDirectory: (NSString *) fileName
{
//NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:fileName];
NSFileManager * fileManager = [NSFileManager defaultManager];
BOOL succeeded = [fileManager fileExistsAtPath:writablePath];
NSError *error;
//If file is not in the documents directory then only write
if (!succeeded)
{
NSString *newPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
succeeded = [fileManager copyItemAtPath:newPath toPath:writablePath error:&error];
if (succeeded == FALSE) {
NSLog(#"%# : copy failed", fileName);
} else {
NSLog(#"%# : copy success", fileName);
}
} else {
NSLog(#"%# : already exists", fileName);
}
}
This is the method for saving the PDF into the NSDocumentDirectory, which has not been changed from the other app...
- (NSString*)saveJournalToPDF:(UIView*)journal andName:(NSString*)name
{
NSString* fileName = [NSString stringWithFormat:#"%#.pdf",name];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:fileName];
... // the rest of the data strings for creating the PDF
}
My question: how do I get ONLY the PDFs to be visible to the user without exposing the other data files? Right now, it seems that it is either all or nothing!
I neglected one small detail... setting the app to actually share files! The app's info.plist did not have "Application supports iTunes file sharing" set to YES. (UIFileSharingEnabled). Such a simple fix!!!
I saved an image in the documents directory, but when I tried to read it, it appears that the file wasnt saved at all.
The read path is OK, the same as the save one.
[_IDBanners addObject:#"44"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString * name = [NSString stringWithFormat:#"%#.png",[_IDBanners objectAtIndex:i]];
NSString *filePath = [documentsPath stringByAppendingPathComponent:name]; //Add the file name
NSData *pngData = [NSData dataWithContentsOfFile:filePath];
UIImage *image = [UIImage imageWithData:pngData];
When I log:
NSLog(#"%#",image);
it returns (null).
I'm having an issue with loading existing images from the App's Documents Folder after ad-hoc update.
I've searched the internet for answers and I've found that I must use relative paths to files in order for the path to remain the same when the App is updated.
Can someone please show me how to do it ?
Right now I use the following to save the files (images) when ImagePicker finishes:
NSString *imageFilename = [NSString stringWithFormat:#"%#-profilePhoto.jpg",[NSDate date]];
NSArray *paths = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
imagePath = [NSString stringWithFormat:#"%#/%#", paths, imageFilename];
Please help me out !
I've found the solution:
Instead of loading the images from the full path, I chose to make the system search for them after their name.
So instead of:
//Full path stored in a dictionary:
profilePhotoPath = [userDict objectForKey:#"profilepic"];
//Load the image from path:
profilePhoto = [[UIImage alloc] initWithContentsOfFile:profilePhotoPath];
I now use the following:
//Load image from documentsDirectory, filename
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
profilePhotoPath = [userDict objectForKey:#"profilepic"];
profilePhotoPath = [documentsDirectory stringByAppendingPathComponent:[profilePhotoPath lastPathComponent]];
profilePhoto = [[UIImage alloc] initWithContentsOfFile:profilePhotoPath];
By using "lastPathComponent" attribute I'm basically stripping out everything from the path except the filename and then I use NSSearch to give me my file.
This line makes the problem
NSArray *paths = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
Here the issue is the variable paths is of type NSArray not NSString
Change it to,
NSString *paths = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
or
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *paths = [path objectAtIndex:0];
I'm really struggling with this,Day three of the unending quest to save and load an image in my app. I'm picking an image from camera roll and trying to save it to the device via the appsdocsdirectory.
in the Appdelegate.m I have:
// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
}
in the class I want to save and load the image (picked from camera roll into UI imageView)
- (IBAction)save:(id)sender {
UIImage *myImage = [imageView image];
NSData *data = UIImagePNGRepresentation(myImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *appDocsDirectory = [paths objectAtIndex:0];
}
- (IBAction)load:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *appDocsDirectory = [paths objectAtIndex:0];
UIImage* thumImage = [UIImage imageWithContentsOfFile: [NSString stringWithFormat:#"%#/%#.png", appDocsDirectory, #"myNewFile"]];
}
#end
I also imported Appdelegate.h, in this class, not sure if that was needed or correct?Now I have finally got rid of all errors and no exceptions being thrown but now my problem is nothing happens when I try to save the image and load it. I'm also getting yellow triangles telling me i have unused veriables in load UIImage* thumImage NSString *appDocsDirectory & NSData *data so I may have made a royal hash of this.
This is how I did it in one of my project.
I get the picture with the delegate method ot the imagePickerController :
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
pictureInfo = info;
}
Then I save it like this :
-(void)createPin
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pinFolderName = [NSString stringWithFormat:#"%f",[[NSDate date] timeIntervalSince1970]];
NSString *pinFolderPath = [documentsDirectory stringByAppendingPathComponent:pinFolderName];
[fileManager createDirectoryAtPath:pinFolderPath withIntermediateDirectories:YES attributes:nil error:NULL];
self.fullsizePath = [pinFolderPath stringByAppendingPathComponent:#"fullsize.png"];
NSString *mediaType = [pictureInfo objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:#"public.image"]){
UIImage *fullsizeImage = [pictureInfo objectForKey:UIImagePickerControllerEditedImage];
NSData *fullsizeData = UIImagePNGRepresentation(fullsizeImage);
[fullsizeData writeToFile:self.fullsizePath atomically:YES];
}
}
Hope you can start from there.
If you need any explanations or help, feel free to ask
I have used NSuser defaults, all sorted
When I save image to file it save with wrong orientation .
- (void)saveImage:(UIImage *)image:(NSString *)imageName
{
NSData *imageData = UIImagePNGRepresentation(image);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat:#"%#.png", imageName]];
[fileManager createFileAtPath:fullPath contents:imageData attributes:nil];
}
Thanks.
Try saving as a jpeg instead:
NSData *imageData = UIImageJPEGRepresentation(image,1.0);
If you need to save it as a PNG, check this discussion:
UIImagePNGRepresentation ..... writeToFile is always landscape