2011-07-21 66 views
6

我想檢查folder.if我在「路徑」發現「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

回答

14

它已經回答here

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

看我的編輯。 THKS – XcodeMania

2

如果指定的文件不存在,UIImage的CGImage屬性將爲零。

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