How to check if a file exists in Documents folder?

后端 未结 7 1294
小蘑菇
小蘑菇 2020-11-29 14:53

I have an application with In-App Purchase, that when the user buy something, download one html file into the Documents folder of my app.

Now I must check if this HT

相关标签:
7条回答
  • 2020-11-29 15:55
    NSArray *directoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *imagePath =  [directoryPath objectAtIndex:0];
    //If you have superate folder
    imagePath= [imagePath stringByAppendingPathComponent:@"ImagesFolder"];//Get docs dir path with folder name
    _imageName = [_imageName stringByAppendingString:@".jpg"];//Assign image name
    imagePath= [imagePath stringByAppendingPathComponent:_imageName];
    NSLog(@"%@", imagePath);
    
    //Method 1:
    BOOL file = [[NSFileManager defaultManager] fileExistsAtPath: imagePath];
    if (file == NO){
        NSLog("File not exist");
    } else {
        NSLog("File exist");
    }
    
    //Method 2:
    NSData *data = [NSData dataWithContentsOfFile:imagePath];
    UIImage *image = [UIImage imageWithData:data];
    if (!(image == nil)) {//Check image exist or not
        cell.photoImageView.image = image;//Display image
    }
    
    0 讨论(0)
提交回复
热议问题