IOS detects the existence of a file for the specified path
- 2020-05-09 19:21:44
- OfStack
- (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,@" Directory creation failed ");
NSString *result = [path stringByAppendingPathComponent:file];
return result;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// The image access path is first specified here (written to the application sandbox by default) C)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
// And give the file a name
NSString *imageDir = [[[paths objectAtIndex:0] stringByAppendingPathComponent:@"163"] stringByAppendingPathComponent:@"songzi"];
// A folder to store pictures in
NSString *imagePath =[imageDir stringByAppendingPathComponent:@" The file name .png"];
NSData *data = nil;
// Check if the image has been saved locally
if([self isExistsFile:imagePath]){
data=[NSData dataWithContentsOfFile:imagePath];
}else{
data = [NSData dataWithContentsOfURL:[NSURL URLWithString: @" The url "]];
// Create folder path
[[NSFileManager defaultManager] createDirectoryAtPath:imageDir withIntermediateDirectories:YES attributes:nil error:nil];
// Create a picture
[UIImagePNGRepresentation([UIImage imageWithData:data]) writeToFile:imagePath atomically:YES];
}
imageView.image = [UIImage imageWithData:data];
}
Check if the file exists
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@""];
if(path==NULL)
Method 2:
NSFileManager *fileManager = [NSFileManager defaultManager];
//Get documents directory
NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [directoryPaths objectAtIndex:0];
if ([fileManager fileExistsAtPath:@""]==YES) {
NSLog(@"File exists");
}
Method 3:
// Determine if the file exists
if(![c judgeFileExist:@"user.plist"])
{
NSLog(@" Please confirm if the file exists! ");
return;
}
Method 4:
// Determine if the file exists
-(BOOL)judgeFileExist:(NSString * )fileName
{
// Get file path
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@""];
if(path==NULL)
return NO;
returnYES;
}