Babul Babul - 5 months ago 39
iOS Question

How to get the local saved UIImage path to UIImage?

I am saving my image by using below code ---

#define DOCUMENTS_FOLDER [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]


NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0];
NSString *caldate = [now description];


NSString *filePath= [NSString stringWithFormat:@"%@/%@.jpg", DOCUMENTS_FOLDER,caldate];
imageurl = [NSURL fileURLWithPath:filePath];
dataImage = UIImagePNGRepresentation(myimage);
[dataImage writeToFile:filePath atomically:YES];


imageurl is printing like below


file://localhost/Users/varmab/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/E2EDF729-4684-465B-8BE3-2C0ACFA2EC03/Documents/2013-03-18%2012:02:31%20+0000.jpg


Now, i want to show the saved image , i.e., image is stored in the above file path

I tried the below codes:

1st process:
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageurl]];

2nd process:
NSURL *urlString =imageurl;
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation([UIImage imageWithData:[NSData dataWithContentsOfURL:urlString]])];
UIImage *image = [UIImage imageWithData:imageData];

3rd process:
NSData * imageData = [[NSData alloc] initWithContentsOfURL:imageurl ];
self.testingImageView.image = [UIImage imageWithData: imageData];


Please guide me, am i doing any mistake?

Thanks in advance

Answer

Why dont you directly use this method:

 - (id)initWithContentsOfFile:(NSString *)path

It is already available in UIImage class.

[self.testingImageView setImage: [[UIImage alloc] initWithContentsOfURL:imageurl]];

Try this, and let me know if you face same issue again.

Note: check the image url whether its similar to the older one, which you are getting while saving that image.

Comments