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?

3
  • please refer this stackoverflow.com/questions/7440662/… Commented Mar 18, 2013 at 13:49
  • Use the NSFileManager Commented Mar 18, 2013 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 Commented Mar 18, 2013 at 13:51

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

i am getting, instead of initWithContentsOfURL , i am getting initWithContentsOfFile
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?
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]]; 

Comments

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)
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.