6

i want to check a folder.if i found "test.jpeg" in "path" if it 's true i do nothing but if it false i have to download this picture like that

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 try this but don't work. the path 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

XcodeMania
  • 305
  • 5
  • 20

2 Answers2

14

Its already answered here.

BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:somePath];
Community
  • 1
  • 1
Praveen S
  • 10,355
  • 2
  • 43
  • 69
2

If the file specified doesn't exist the CGImage property of UIImage will be nil.

if (image.CGImage) NSLog(@"file ok");
else NSLog(@"file ko");
RTasche
  • 2,614
  • 1
  • 15
  • 19