0

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.

MarmiK
  • 5,639
  • 6
  • 40
  • 49
Meera
  • 309
  • 2
  • 14
  • What does "I am unable" mean? You wrote some code - does it fail? Could you err-msgs? Also pls. reformat the code to make it easier to read the q. – MBaas Sep 16 '15 at 11:21
  • I've edited your question to fix the formatting, but you need to include a lot more detail to allow others to help you. Have a look at "[*How do I ask a good question?*](http://stackoverflow.com/help/how-to-ask)" for some tips. – Wai Ha Lee Sep 16 '15 at 11:24
  • sorry, I am new to here. Actually I was asking that is this code okay? I can not get image from image path UIImage *thumbNail = [[UIImage alloc] initWithContentsOfFile:getImagePath]; , getting nil image . I don`t know at where i m lacking . – Meera Sep 16 '15 at 11:28
  • Try debugging it. Use breakpoints and see what the values are for things like `documentsDirectory` and `getImagePath`. Look in the app's sandbox to see if there's really a file at the location you're trying to read. – Phillip Mills Sep 16 '15 at 12:07
  • documentDirectory :"/var/mobile/Containers/Data/Application/3F4B4215-11EB-4269-BA65-97DCC32ACAF6/Library/Documentation " and getImagePath :"/var/mobile/Containers/Data/Application/3F4B4215-11EB-4269-BA65-97DCC32ACAF6/Library/Documentation/1442405926.png" get while i am saving image and getImagePath:"/var/mobile/Containers/Data/Application/3F4B4215-11EB-4269-BA65-97DCC32ACAF6/Library/Documentation/1442400637.png" get while retrieving image but initWithContentsOfFile: returns nil image. – Meera Sep 16 '15 at 12:26
  • replace `NSDocumentationDirectory` with `NSDocumentDirectory` in `NSSearchPathForDirectoriesInDomains` function. i hope you got my point. – Dipen Panchasara Sep 16 '15 at 13:07
  • I replace It and its working!! superb !! :) thank you so much @DipenPanchasara. Thanks everyone :) – Meera Sep 16 '15 at 13:20
  • @Meera you are welcome. – Dipen Panchasara Sep 16 '15 at 13:24

2 Answers2

2
-(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;
}
Meera
  • 309
  • 2
  • 14
0

Seems like typo in your directory name. Please replace NSDocumentationDirectory with NSDocumentationDirectory and it should work.

Abhinav
  • 37,684
  • 43
  • 191
  • 309