Check if file exists

I want to check the folder. If I found "test.jpeg" in "path", if it is true, I do nothing, but if it is false, I need to upload this image like this

UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[dico objectForKey:@"photo"]]]]; nomPhoto = [[cell.pseudo text]stringByReplacingOccurrencesOfString:@"\n" withString:@""];; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString *document = [paths objectAtIndex:0]; filename = [NSString stringWithFormat:@"%@/%@.jpeg",document,nomPhoto]; NSData *data2 = [NSData dataWithData:UIImageJPEGRepresentation(image, 0.1f)];//1.0f = 100% quality [data2 writeToFile:filename atomically:YES]; 

EDIT: I'm trying this, but not working. the way is good

 NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString* pict = [documentsPath stringByAppendingPathComponent :@"portos"]; BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:pict]; if (fileExists) { NSLog(@"file ok"); }else { NSLog(@"file ko"); } 

THX

+6
source share
2 answers

He has already been answered here .

 BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:somePath]; 
+14
source

If the specified file does not exist, the CGImage property for UIImage will be nil.

 if (image.CGImage) NSLog(@"file ok"); else NSLog(@"file ko"); 
+2
source

Source: https://habr.com/ru/post/893240/


All Articles