Search code examples
iosnsdocumentdirectory

the image is not retrieving from document directory


i have image in document directory with name 1.png. and i want to display that image in imageview. i used the code below but the image is not displaying in imageview.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
NSLog(@"%@",paths);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:@"1.png"];
UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];
NSLog(@"%@",getImagePath);
imge.image=img;

Solution

  • 1 - What is the result of paths, documentsDirectory and getImagePath is correct or not.

    2 - Try set the img filename manually (ie : @"1.png") and check if img is loaded correctly

    3 - At the other hand, check the file if it exists in documentsDirectory path (also included by project)

    NSURL *url = [NSURL fileURLWithPath:path];
    

    Check if file is exists in your documents directory or not

    if([[NSFileManager defaultManager] fileExistsAtPath:path])
    NSURL *url = [NSURL fileURLWithPath:path];
    

    Also check in your documents directory if the image is getting saved or not. You are given the name of the image like this

    image = [UIImage imageNamed:@"1.png"];
    

    so if you are repeating this line your images will be overwritten in documents directory.

    You can also put a check here

    BOOL success = [data writeToFile:path atomically:YES];
    
      if(success)
      NSLog(@"successfully");
      else
      NSLog(@"failed");