Babul Babul -5 years ago 218
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


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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download