xcode 检查文件是否存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6772872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
check if file exist
提问by XcodeMania
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
我想检查一个文件夹。如果我在“路径”中找到了“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];
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
谢谢
回答by Praveen S
回答by RTasche
If the file specified doesn't exist the CGImage property of UIImage will be nil.
如果指定的文件不存在,则 UIImage 的 CGImage 属性将为 nil。
if (image.CGImage) NSLog(@"file ok");
else NSLog(@"file ko");