This article to share is in the development of iOS to detect the existence of a specified file method, for everyone summed up 4 kinds, very practical, small partners according to their own needs free choice.
The code is as follows:
-(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 directory Failed");
NSString *result = [path stringbyappendingpathcomponent:file];
return result;
}
-(void) viewdidload
{
[Super Viewdidload];
The picture access path is first specified (written in the application sandbox by default)
Nsarray *paths = Nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask, YES);
and give the file a filename
NSString *imagedir = [[[Paths objectatindex:0] stringbyappendingpathcomponent:@ "163"] stringbyappendingpathcomponent : @ "Songzi"];
folder where pictures are stored
NSString *imagepath =[imagedir stringbyappendingpathcomponent:@ "filename. png"];
NSData *data = nil;
Check to see if the picture has been saved locally
if ([self isexistsfile:imagepath]) {
Data=[nsdata Datawithcontentsoffile:imagepath];
}else{
data = [NSData datawithcontentsofurl:[nsurl urlwithstring: @ "url"]];
Create a 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 to see if a file exists
The code is as follows:
NSString *path = [[NSBundle mainbundle] Pathforresource:filename oftype:@ "];
if (path==null)
Method Two:
The code is as follows:
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 Three:
The code is as follows:
To determine whether a file exists
if (![ C judgefileexist:@ "User.plist"])
{
NSLog (Please confirm the file exists!);
Return
}
Method Four:
The code is as follows:
To determine whether a file exists
-(BOOL) Judgefileexist: (NSString *) fileName
{
Get file path
NSString *path = [[NSBundle mainbundle] Pathforresource:filename oftype:@ "];
if (path==null)
return NO;
Returnyes;
}