1

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?

Krunal
  • 77,632
  • 48
  • 245
  • 261
Babul
  • 1,268
  • 2
  • 15
  • 42
  • please refer this http://stackoverflow.com/questions/7440662/how-to-get-all-paths-for-files-in-documents-directory – Vinodh Mar 18 '13 at 13:49
  • Use the NSFileManager – Rushabh Mar 18 '13 at 13:49
  • i did like this but, i got null------- NSString * path = [[NSBundle mainBundle] pathForResource:@"images" ofType:@"jpeg"]; imgUlr=[NSURL URLWithString:path]; NSLog(@"********* testingImg is %@",imgUlr); @Rushabh – Babul Mar 18 '13 at 13:51

3 Answers3

4

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.

Mrunal
  • 13,982
  • 6
  • 52
  • 96
  • i am getting, instead of initWithContentsOfURL , i am getting initWithContentsOfFile – Babul Mar 18 '13 at 13:57
  • Why I am referring this method, because it will reduce your lines of code. And if you are satisfied with the same then can you please accept and upvote the answer? – Mrunal Mar 18 '13 at 14:28
1

This will give you the paths for all files under the documents directory;

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];

        NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];

        NSLog(@"files array %@", filePathsArray);

You can get the path for each object in filePathsArray by using the below code;

    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[filePathsArray objectAtIndex:0]]; 
Mert Celik
  • 665
  • 8
  • 12
Vinodh
  • 5,262
  • 4
  • 38
  • 68
0

Swift
Get Path of document directory, store image there and from same location, you can retrieve.

func getDocumentsDirectory() -> URL {
    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    let documentsDirectory = paths[0]
    return documentsDirectory
}

if let image = UIImage(named: "example.png") {
    if let data = UIImagePNGRepresentation() {
        let filename = getDocumentsDirectory().appendingPathComponent("copy.png")
        try? data.write(to: filename)
    }
}
Krunal
  • 77,632
  • 48
  • 245
  • 261