I am unable to save and retrieve image in iOS. I am getting nil image trough this code:
-(NSString *)saveImageDataWithImage:(UIImage *)image
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *photoName = [NSString stringWithFormat:#"%ld.png",
(long)[[NSDate date] timeIntervalSince1970]];
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:photoName];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:getImagePath])
{
NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)];
[data1 writeToFile:getImagePath atomically:YES];
}
return photoName;
}
-(UIImage *)GetImageFromPath:(NSString *)ImageName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:ImageName];
UIImage *thumbNail = [[UIImage alloc] initWithContentsOfFile:getImagePath];
return thumbNail;
}
Why I am getting nil image?
Please help me.
-(NSString *)saveImageDataWithImage:(UIImage *)image
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory ,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *photoName = [NSString stringWithFormat:#"%ld.png",
(long)[[NSDate date] timeIntervalSince1970]];
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:photoName];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:getImagePath])
{
NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)];
[data1 writeToFile:getImagePath atomically:YES];
}
return photoName;
}
-(UIImage *)GetImageFromPath:(NSString *)ImageName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory ,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:ImageName];
UIImage *thumbNail = [[UIImage alloc] initWithContentsOfFile:getImagePath];
return thumbNail;
}
Seems like typo in your directory name. Please replace NSDocumentationDirectory with NSDocumentationDirectory and it should work.
Related
I am using following code for getting single image. But, how to do it for multiple images?
This is my code :
-(void)viewDidLoad
{
[super viewDidLoad];
[self performSelectorInBackground:#selector(loadImageIntoMemory) withObject:nil];
}
-(void)loadImageIntoMemory {
NSLog(#"Beginning download");
NSString *temp_Image_String = [[NSString alloc] initWithFormat:#"http://www.thewavestore.com/appproductimage/"];
NSURL *url_For_Ad_Image = [[NSURL alloc] initWithString:temp_Image_String];
NSData *data_For_Ad_Image = [[NSData alloc] initWithContentsOfURL:url_For_Ad_Image];
UIImage *temp_Ad_Image = [[UIImage alloc] initWithData:data_For_Ad_Image];
[self saveImage:temp_Ad_Image];
UIImageView *imageViewForAdImages = [[UIImageView alloc] init];
imageViewForAdImages.frame = CGRectMake(0, 0, 320, 480);
imageView.image = [self loadImage];
[self.view addSubview:imageView];
}
-(void)saveImage:(UIImage *)image {
NSLog(#"Saving");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent: #"DSC02077129HT41.5W20L24WT18.555RS28775.jpg" ];
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
}
-(UIImage *)loadImage {
NSLog(#"Got the data!");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:#"DSC02077129HT41.5W20L24WT18.555RS28775.jpg" ];
UIImage* image = [UIImage imageWithContentsOfFile:path];
return image;
}
Please help to over come from this issues,if any good idea please suggest me.
First save your images with unique names (i am using current time) and save this names to an array
NSData *imageData = UIImagePNGRepresentation(chosenImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
// save image with currentdate and time in the name
NSString * imageName = [NSString stringWithFormat:#"name_%#.png",[dateFormatter stringFromDate:[NSDate date]]];
NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
//add all the images names to the imagepath array
[imagePathArray addObject:imageName];
//write image into file
[imageData writeToFile:fullPathToFile atomically:YES];
then retrive images from image path array
for (NSString*path in imagePathArray ) {
NSArray *filepart = [path componentsSeparatedByString:#"."];
NSString *filename = [filepart objectAtIndex:0];
NSString *extension = [filepart objectAtIndex:1];
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [NSString stringWithFormat:#"%#/%#",
documentsDirectory,path];
//NSString *filePath = [[NSBundle mainBundle] pathForResource:filename ofType:extension];
NSData *fileData = [NSData dataWithContentsOfFile:fileName];
}
1. Store the server images in to array.
NsMutableArray *arrImages=[[nsmutableArray alloc]init];
2.Get the count of the array to run this in for loop.
for(int i=0;i<[arrImages count];i++)
{
NSLog(#"Saving");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent: arrImages[i]];
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
}
To convert it to JPEG
NSData=UIImageJPEGRepresentation(UIImage,1.0);
I want to save and load image in UIImageView
I have already tried some code but it doesn't work.
The Code i have tried:
Save:
UIImage *image = ...;
NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:#"image.png"];
[UIImagePNGRepresentation(image) writeToFile:cachedImagePath atomically:YES];
Load:
NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:#"image.png"];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfFile:cachedImagePath]];
But these code doesn't work!
I hope you want to save image in document directory folder
here is the way to save image:
- (void)saveImage: (UIImage*)image
{
if (image != nil)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithString: #"test.png"] ];
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
}
}
to get image from document directory:
- (UIImage*)loadImage
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithString: #"test.png"] ];
UIImage* image = [UIImage imageWithContentsOfFile:path];
return image;
}
Hope this will help
Globally declare imageView
UIImageView *imageView;//if u create imageView in interfaceBuilder. then u can replace this line by creating outlet of ur imageView
for loading image:
//Loading image in imageView
NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:#"image.png"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:cachedImagePath])
{
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfFile:cachedImagePath]];
if (image)
{
imageView = [[UIImageView alloc]init];//Dont do this, if u created imageView using interFace builder
[imageView setFrame:CGRectMake(50, 100, 200, 200)];//Dont do this, if u created imageView using interFace builder
[self.view addSubview:imageView];//Dont do this, if u created imageView using interFace builder
[imageView setImage:image];
}
else
{
NSLog(#"Unsupported File");
}
}
else
{
NSLog(#"Image not Found in the path");
}
for saving image:
UIImage *image = imageView.image;
if (image)
{
NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:#"imageSaved.png"];
[UIImagePNGRepresentation(image) writeToFile:cachedImagePath atomically:YES];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:cachedImagePath])
{
NSLog(#"File Created");
}
}
else
{
NSLog(#"No image to save");
}
First I stored image path from web service into SQLITE, and then I retrieve it back using local path. I am using NSMutableArray. I've successfully stored one image in NSDocumentDirectory, but I have one issue, How can I store more than one images in NSDocumentDirectory?
My code is,
if ([[[node childAtIndex:counter] name] isEqualToString:#"Column5"])
{
NSString *str = [[node childAtIndex:counter] stringValue];
[Col5 addObject:str];
NSString *myString = [Col5 componentsJoinedByString:#","];
NSString *aString = [NSString stringWithFormat:#"http://webserviceurl/%#",myString];
NSURL *url= [NSURL URLWithString:aString];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:strCol4];
UIImage *image = tmpImage;
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];
}
For this line:
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:strCol4];
Just use a different path component.
Usually when you have the images in the project file you use the code
arraycollectionimages = [[NSArray alloc]]initwithobjects
however I want to show the images that are saved in the directories. I have created multiple directories so I just want to recall image for one of them. To make this work I want to use this code however I cannot do this? I just stuck it it in as follows but did not work well.
- (void)viewDidLoad {
NSArray *arrayCollectionImages = [[NSArray alloc ]init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *location=#"Genre1";
NSString *fPath = [documentsDirectory stringByAppendingPathComponent:location];
NSArray *directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: fPath];
for(NSString *str in directoryContent){
NSString *finalFilePath = [fPath stringByAppendingPathComponent:str];
NSData *data = [NSData dataWithContentsOfFile:finalFilePath];
if(data)
{
UIImage *image = [UIImage imageWithData:data];
[arrayCollectionImages addObject:image];
}
}
I think your directory search is probably taking long. Try this...
- (void)viewDidLoad {
dispatch_queue_t searchQ = dispatch_queue_create("com.awesome", 0);
dispatch_async(searchQ, ^{
NSArray *arrayCollectionImages = [[NSArray alloc ]init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *location=#"Genre1";
NSString *fPath = [documentsDirectory stringByAppendingPathComponent:location];
NSArray *directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: fPath];
for(NSString *str in directoryContent){
NSString *finalFilePath = [fPath stringByAppendingPathComponent:str];
NSData *data = [NSData dataWithContentsOfFile:finalFilePath];
if(data)
{ dispatch_async(dispatch_get_main_queue(),^{
UIImage *image = [UIImage imageWithData:data];
});
[arrayCollectionImages addObject:image];
}
}
});
}
The code basically spawns off another thread on the search and puts the image when its ready.
How do I retrieve image data from plist and display to that data to an image view?
- (void)getImages {
// Look in Documents for an existing plist file
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
myPlistPath = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat: #"%#.plist", plistName] ];
[myPlistPath retain];
// If it's not there, copy it from the bundle
NSFileManager *fileManger = [NSFileManager defaultManager];
if ( ![fileManger fileExistsAtPath:myPlistPath] ) {
NSString *pathToSettingsInBundle = [[NSBundle mainBundle]
pathForResource:plistName ofType:#"plist"];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *path = [documentsDirectoryPath
stringByAppendingPathComponent:#"myApp.plist"];
NSMutableDictionary *plist = [NSDictionary dictionaryWithContentsOfFile: path];
NSArray *imageArray = [plist objectForKey:#"imageArray"];
for (NSString *imageString in imageArray) {
DLog(#"Image Name: %#", imageString);
}
}
Get plist code from www.iphoneexamples.com