我一直在尝试使用AVFoundation记录屏幕输出。由于未知的原因,在我移至最新版本的Mac(Mountain Lion)后,它停止了工作。我一直在努力使其工作,但到目前为止还没有取得成果。我知道如果输出文件已经存在,AVFoundation方法startRecordingToOutputFileURL将不起作用。因此,我尝试使用NSFileManager来查看我的目标文件是否存在以及是否可写。我的Filemanager总是返回对应于目标文件不存在且不可写的值。我试图将文件权限设置为无效,任何人都可以对我可能的错误有所了解:

dest = [[NSURL alloc] initFileURLWithPath:@"~/Desktop/myMovie.mov"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
[attributes setObject:[NSNumber numberWithInt:777] forKey:NSFilePosixPermissions]; //I tried 511 too, no avail
[fileManager setAttributes:attributes ofItemAtPath:[dest path] error:nil];
if (![fileManager fileExistsAtPath:[dest path]]) {
     if ([fileManager isWritableFileAtPath:[dest path]]) {
            /* Starts recording to a given URL. */
         [captureMovieFileOutput startRecordingToOutputFileURL:dest recordingDelegate:self];
        }
        else{
            NSLog(@"File doesnot exist but is not writable"); //This is the message I get as result
        }

    }
    else
    {
        NSLog(@"File Exists...");
    }

最佳答案

未展开的波浪线在Cocoa中不是有效的路径。您必须在传递到-stringByExpandingTildeInPath-stringByStandardizingPath的字符串上使用NSURL或更好的-initFileURLWithPath:

因此,NSFileManager将为isWritableFileAtPath返回NO,因为它是无效路径(因此它不可写)。这将导致您的NSLog()被解雇。

根据评论更新:

您可能仍然发现NSURL在创建时返回nil(因此,调用-path将返回nil),因为该路径仍然无效。另外值得一提的是,该文档对-isWritableFileAtPath说:“尝试进行操作(例如加载文件或创建目录),检查错误并优雅地处理这些错误比尝试预先找出要好得多。时间是否会成功。”

采纳Peter Hosey的建议,如果在尝试写入文件时调用失败,并且不要试图提前弄清楚它,请使用NSError。

关于macos - NSFileManager说文件不可写Mac,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14072398/

10-14 21:09
查看更多