Image does not load from file path in iOS

I want to download an image from the saved file path of this image, which is stored on my iPhone. I get the path, but still it says the file does not exist. This is my code:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                             NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",self.imageName]];
        BOOL fileExists=[[NSFileManager defaultManager] fileExistsAtPath:imagePath];
        NSLog(@"%@",imagePath);
        UIImage *fetchImage = [UIImage imageWithContentsOfFile:imagePath];

fileExists returns NO even when I get this path:

/var/mobile/Applications/2ED5C185-8528-48D3-BE0B-28582DC3D294/Documents/IMG_0028.JPG

+3
source share
6 answers

I ran into this problem. Hope this saves someone a headache in the future.

You cannot rely on a catalog of documents to stay consistent from one assembly to the next. When you redeploy the application, this path may change:

/var/mobile/Applications/2ED5C185-8528-48D3-BE0B-28582DC3D294/Documents/

, JSON . :

2ED5C185-8528-48D3-BE0B-28582DC3D294

. .

+7

,

NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
      NSString *imageName = [[NSString alloc]initWithFormat:@"%@",self.imageName ];
      NSString* foofile = [documentsPath stringByAppendingPathComponent:imageName];
      BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:foofile];
      if (fileExists) {
        NSLog(@"alreday Exists");
       }

//

NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pngFilePath = [NSString stringWithFormat:@"%@/%@", docDir,self.imageName];
UIImage *img = [UIImage imageWithContentsOfFile:pngFilePath];
+1

try the following:

NSString *strNew=[dci valueForKey:@"uThumbPath"];
    NSString *strCompare=[NSString stringWithFormat:@"%@.jpg",[self MD5String:strNew]];
    NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSLog(@"%@",documentsPath);
    NSString *strFileName=[NSString stringWithFormat:@"%@/%@",documentsPath,strCompare];

    if([[NSFileManager defaultManager] fileExistsAtPath:strFileName])
    {
        NSLog(@"Downloaded From Localpath");
        NSString *inputPath= strFileName;
        NSString *extension=[inputPath pathExtension];
        NSString *file=[[inputPath lastPathComponent]stringByDeletingPathExtension];
        NSString *dir=[inputPath stringByDeletingLastPathComponent];
        NSString *imagePATH=[NSBundle pathForResource:file ofType:extension inDirectory:dir];
       UIImage* theImage=[UIImage imageWithContentsOfFile:imagePATH];
        cell.imgThumb.image=theImage;

    }
    else{
        NSLog(@"Downloaded From Server");
        NSURL *URL = [NSURL URLWithString: [dci valueForKey:@"uThumbPath"]];
        NSURLRequest* request = [NSURLRequest requestWithURL:URL];
        NSLog(@"%@",strNew);
        [NSURLConnection sendAsynchronousRequest:request
                                           queue:[NSOperationQueue mainQueue]
                               completionHandler:^(NSURLResponse * response,
                                                   NSData * data1,
                                                   NSError * error) {

                                   if (!error){

                                       [self saveLocally:data1 name:strNew];
                                       cell.imgThumb.image = [UIImage imageWithData:data1];

                                   }

                               }];

    }

and use this

- (NSString *)MD5String:(NSString *)string{
    const char *cstr = [string UTF8String];
    unsigned char result[16];
    CC_MD5(cstr, strlen(cstr), result);

    return [NSString stringWithFormat:
            @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];  
}
+1
source
UIImage *image =[UIImage imageWithData:[NSData dataWithContentsOfFile:self.aStrImagePath]];
0
source

try with the following code:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.JPG",self.imageName]];
    BOOL fileExists=[[NSFileManager defaultManager] fileExistsAtPath:imagePath];
    NSLog(@"%@",imagePath);
    UIImage *fetchImage = [UIImage imageWithContentsOfFile:imagePath];
0
source

It happened to me with me. Xcode did not update my files correctly after adding it to my project. Try to make clean (Product → Clean) and see if the problem is fixed.

-2
source

All Articles