检查文件是否存在

时间:2011-07-21 07:51:47

标签: iphone xcode

我想检查一个文件夹。如果我在“路径”中找到“test.jpeg”,如果它是真的我什么都不做,但如果它是假的我必须下载这样的图片

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];
编辑:我试试这个但是不行。路径很好

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

2 个答案:

答案 0 :(得分:14)

已经回答here

BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:somePath];

答案 1 :(得分:2)

如果指定的文件不存在,UIImage的CGImage属性将为nil。

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