考虑以下两个代码示例:
NSData *imgData = UIImagePNGRepresentation(imgFull);
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"itemImg_%i.png", timestamp]]; //add our image to the path
[imgData writeToFile:fullPath atomically:YES];
和
NSData *imgData = UIImagePNGRepresentation(imgFull);
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"itemImg_%i.png", timestamp]]; //add our image to the path
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createFileAtPath:fullPath contents:imgData attributes:nil];
第二个示例需要额外的一行代码和NSFileManager对象的初始化,而第一个示例只是让NSData对象imgData
将自身写入到一个文件中。第一个示例的另一个优点是,它可以覆盖具有相同名称的预先存在的文件。
我的问题是:在创建新文件时,在什么情况下实际需要使用NSFileManager及其方法createFileAtPath:contents:attributes:
https://stackoverflow.com/questions/9466865
复制相似问题