I have an app that I am saving images to the NSDocumentDirectory. I can see that the path is being set correctly but when I try to retrieve them the NSData is null.
Here are some of my snippets..
NSData *imageData = UIImagePNGRepresentation(image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"myApp"];
//Create unique filename
CFUUIDRef newUniqueId = CFUUIDCreate(kCFAllocatorDefault);
CFStringRef newUniqueIdString = CFUUIDCreateString(kCFAllocatorDefault, newUniqueId);
path = [path stringByAppendingPathComponent:(__bridge_transfer NSString *)newUniqueIdString];
path = [path stringByAppendingPathExtension: #"png"];
[imageData writeToFile:path atomically:NO];
[self showPhoto:path];
Then in my showPhoto method
NSData *pngData = [NSData dataWithContentsOfFile:path];
UIImage *image = [UIImage imageWithData:pngData];
If I debug pngData it is 0 bytes but my path show what I think is the correct path?
Any way of debugging if the photo is actually on the phone? Im not sure if it is getting saved or not.
You can do something like this and check if the file exists first and use imageWithContentsOfFile: to retrieve it.
NSString *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:localFilePath];
bool fileExists = [[NSFileManager defaultManager] fileExistsAtPath:imagePath];
if(fileExists == YES)
{
UIImage *_image = [UIImage imageWithContentsOfFile:localFilePath]
}
If the image is still empty then there is something wrong with your image.
If you running app in simulator you can find application files here: /Users/{user}/Library/Application Support/iPhone Simulator/5.0/Applications
If you debugging on iPhone you can use iExplorer to see files on iPhone.
Related
It is my save method :
-(NSString *)saveImage:(UIImage *)image
{
NSInteger RandomIndex = arc4random() % 1000;
NSString *randomImageName =[NSString stringWithFormat:#"Image%i.png",RandomIndex];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:randomImageName];
if ([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]) {
[[NSFileManager defaultManager] removeItemAtPath:savedImagePath error:nil];
NSLog(#"file removed from path");
}
NSLog(#"Saved Image Path : %#",savedImagePath);
NSData* imageData = UIImagePNGRepresentation (image);
[imageData writeToFile:savedImagePath atomically:YES];
self.teamLogo = savedImagePath;
return savedImagePath;
}
I try to load image and put into control :
NSData *myData = [NSData dataWithContentsOfFile:self.team.logo];
UIImage *selectedImage = [UIImage imageWithData:myData];
self.team.logo is from DB. I keep string in base. In debug mode i got this string :
/var/mobile/Containers/Data/Application/7CD69EC9-9C20-48EE-B611-FC2353BC31B0/Documents/Image364.png
and myData is still nil. Do you have idea why ?
Starting from IOS 8 layout of containers changed. So don't store full path to your DB. Instead just store your image name. For recalling the image use:
NSString *temp = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:#"Image364.png"];
NSData *myData = [NSData dataWithContentsOfFile:temp];
UIImage *selectedImage = [UIImage imageWithData:myData];
I hope it helps.
I have an application that receive images with webservices and show them in a list like for example a movie theater schedule
My question is : Is it possible to store the images in core data or something else so i can show them when the user is not connected to internet ?
Yes you can.
// Save image to disk
NSString *documentaryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/Image.png",documentaryPath];
NSData *data = [NSData dataWithData:UIImagePNGRepresentation(YOUR_IMAGE)];
[data writeToFile:filePath atomically:YES];
// Retrieve the Image
- (NSData *) imageData {
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pngFilePath = [NSString stringWithFormat:#"%#/Image.png",docDir];
NSData *dataImage = [NSData dataWithContentsOfFile:pngFilePath];
return dataImage;
}
And later
UIImage *image = [UIImage imageWithData:imageData]
Yes it's possible.
You have to download and save each received image in the application directory, then you save in CoreData the path to those images.
In iOS 8, I am writing UIImages on the device using the code
NSString *documentsDirectory = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
NSString *pathString = [NSString stringWithFormat:#"%#/%#%ld",documentsDirectory, #"ProfileImage", (long)[[NSUserDefaults standardUserDefaults] integerForKey:#"PatientsId"]];
NSData *imageData = UIImageJPEGRepresentation(image, 40);////I have replaced it with UIImagePNGRepresentation as well
[imageData writeToFile: pathString atomically:YES];
The write to file is successful.
If I try to recover the image using the function,
NSArray *documentDirectories =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
// Get one and only document directory from that list
NSString *documentDirectory = [documentDirectories objectAtIndex:0];
NSString *pathString =[documentDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#%ld", #"ProfileImage", (long)[[NSUserDefaults standardUserDefaults] integerForKey:#"PatientsId"]]] ;
UIImage *image = [UIImage imageWithContentsOfFile: pathString];
The image is recovered.
However, if I restart the device, the images are not there anymore. The function [UIImage imageWithContentsOfFile: pathString] returns nil and if I use NSData *myData = [[NSData alloc] initWithContentsOfFile: pathString];, the myData is nil as well.
This procedure was working perfectly fine prior to iOS 8, however, it isn't working properly anymore. Can someone please guide me what is wrong with this?
I am making an app that will save a file to the iPhones Documents Directory and then when the View Controller is reloaded it will fetch it from that same location and display it. But it is not fetching it and displaying it.
I am using the following method to save the image:
void UIImageWriteToFile(UIImage *image, NSString *fileName)
{
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectoryPath = dirPaths[0];
NSString *filePath = [documentDirectoryPath stringByAppendingPathComponent:fileName];
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:filePath atomically:YES];
}
I am trying to recall the image I have saved from the viewDidLoad method with the following code:
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectoryPath = dirPaths[0];
NSString *fileName = [NSString stringWithFormat:#"/%#.png", self.fullname.text];
NSString *filePath = [documentDirectoryPath stringByAppendingString:fileName];
[self.imageView setImage:[UIImage imageNamed: filePath]];
However, it is not showing my image. Can anyone help me and tell me where I am going wrong please. I am new to iOS Development.
replace this line: [self.imageView setImage:[UIImage imageNamed: filePath]];
with this one :
UIImage *image = [[UIImage alloc] initWithContentsOfFile:filePath];
[self.imageView setImage:image];
You can use [UIImage imageNamed:] with images from the Documents folder and have the benefits of the underlying caching mechanism, but the file path is slightly different.
Use the following:
NSString *fileName = [NSString stringWithFormat:#"../Documents/%#.png", self.fullname.text];
UIImage *image = [UIImage imageNamed:fileName];
You need to use method
- (UIImage*)initWithContentsOfFile:(NSString*)path
to init images from Documents or Cache directories.
imageNamed: is used for getting images from application bundle.
I'm using a simple implementation of UIImagePickerController to take a picture and save it to my app directory. I"m running into problems with didFinishPickingMediaWithInfo, where I retrieve the image from the info, convert it into NSData using UIImageJPEGRepresentation, and then save it to a file. Problem is, the NSData object appears never to be overwritten, so the first time I take a picture it saves the picture just fine, but any subsequent pictures are not saved, because when I redisplay the image located at the filePath I'm saving it to, it's always the very first picture I took.
I have NSLog statements right now printing out the hash of the UIImage and of the NSData object I'm saving the image to. The UIImage hash changes each time, but the NSData hash is exactly the same all the time...shouldn't that change too?
My code is below. Note that everything not declared in the delegate method below is a global var that isn't causing me any trouble (at least they don't seem to be).
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory, fileNameString];
NSData *imageData = UIImageJPEGRepresentation(image, 0.5f);
// imageData = [NSData dataWithData:UIImageJPEGRepresentation(image, 0.5f)];
[imageData writeToFile:filePath atomically:YES];
[self dismissModalViewControllerAnimated:YES];
}
When I get rid of the NSData object entirely and replace all instances of
imageData
with
UIImageJPEGRepresentation(image, 0.5f)
I still have the same problem, namely that the hash of UIImageJPEGRepresentation(image, 0.5f) is always the same, even though the hash of the UIImage it takes as a parameter always changes.
Try this:
NSData *imageData = UIImageJPEGRepresentation(image, 0.5f);
imageData = [NSKeyedArchiver archivedDataWithRootObject:imageData];
//imageData = [NSData dataWithData:UIImageJPEGRepresentation(image, 0.5f)];
NSError *error= nil;
[imageData writeToFile:filePath options:NSDataWritingAtomic error:&error];
Also take a look at this link
Are you creating the directory? Use this code to create the directory:
NSArray* dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString* docsDir = [dirPaths objectAtIndex:0];
pathString = [docsDir stringByAppendingPathComponent:#"%#",filePath];
NSFileManager* filemgr = [NSFileManager defaultManager];
//Creates Directory
if ([filemgr createDirectoryAtPath:pathString
withIntermediateDirectories:YES
attributes:nil
error: NULL] == YES){