iphone - 检查文件是否存在

标签 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");
}

谢谢

最佳答案

已经回答了here .

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

关于iphone - 检查文件是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6772872/

相关文章:

iphone - 定义机器系统功能的设计策略建议

ios - 如何在 iphone 5s 和 iphone X 的 Storyboard 中为 uibutton 设置不同的大小?

xcode - 将 GeoFire 与 Firebase 3.2.1 结合使用时出现 "Expected a type"错误

ios - 在 UITableView 中实现 imageView 滑动,具有水平滚动和页面指示器

ios - 在表格 View 中使用两个不同的单元格

ios - xcode-字符串执行错误

iphone - 如何使用 nsurlconnection 发送 post 请求

iphone - 在 sleep 模式下与 iPhone 交互

iphone - 在cocos2D中触摸 Sprite 会导致我的应用崩溃

iphone - iOS UIWebview 忽略 CSS 行高