SoFunction
Updated on 2025-04-11

IOS detects whether the file with the specified path exists


- (NSString *)dataPath:(NSString *)file 

    NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"badge"]; 
    BOOL bo = [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil]; 
NSAssert(bo,@"Create failed");
    NSString *result = [path stringByAppendingPathComponent:file]; 
    return result; 
}  
- (void)viewDidLoad 

    [super viewDidLoad];  
//The image access path is first specified here (by default, written to the application sandbox)
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 
//And give the file a file name
    NSString *imageDir = [[[paths objectAtIndex:0] stringByAppendingPathComponent:@"163"] stringByAppendingPathComponent:@"songzi"]; 
//Folder to store pictures
NSString *imagePath =[imageDir stringByAppendingPathComponent:@"File name.png"];
    NSData *data = nil; 
//Check whether the image has been saved locally
    if([self isExistsFile:imagePath]){ 
        data=[NSData dataWithContentsOfFile:imagePath]; 
    }else{ 
data = [NSData dataWithContentsOfURL:[NSURL URLWithString: @"Website"]];
//Create folder path
        [[NSFileManager defaultManager] createDirectoryAtPath:imageDir withIntermediateDirectories:YES attributes:nil error:nil]; 
//Create a picture
        [UIImagePNGRepresentation([UIImage imageWithData:data]) writeToFile:imagePath atomically:YES];          
    } 
    = [UIImage imageWithData:data];