2013年11月12日 星期二

判斷檔案是否存在document中

NSArray *_paths               = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *_documentsDirectory = [_paths objectAtIndex:0];
    NSString *_filePath         = [_documentsDirectory stringByAppendingPathComponent:@"test.jpg"];
    NSData *_fileData             = nil;
    //先檢查檔案是否存在
    NSFileManager *_fileManager = [NSFileManager defaultManager];
    //存在於 Documents ( 一般從 HTTP 下載的檔案都會先另存在 Documents 裡備用 )
    if( [_fileManager fileExistsAtPath:_filePath] )
    {
        //檔案存在document中做這段
    }
    else
    {
        //檔案不存在document中做這段(看到去其他地方找檔案,或是上網下載)
        //存在於 MainBundle  
        _filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@jpg"];
        _fileData = [[NSData alloc] initWithContentsOfFile:_filePath];

    }

沒有留言:

張貼留言