我一直在看这个几个小时,似乎无法弄清楚我的问题。这是我的代码
NSFileManager* fileManager = [[NSFileManager alloc] init];
NSArray* urls = [fileManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask];
NSArray* directory = [fileManager contentsOfDirectoryAtURL:urls[0] includingPropertiesForKeys:@[@"NSCreatingDateKey"] options:NSDirectoryEnumerationSkipsHiddenFiles error:nil];
NSLog(@"%@", directory);
NSURL* pathURL = [[urls objectAtIndex:0] URLByAppendingPathComponent:@"stanfordpictures/"];
if ([directory count] < 3) {
[fileManager createDirectoryAtURL:pathURL withIntermediateDirectories:NO attributes:nil error:nil];
}
NSString* pictureString = [NSString stringWithFormat:@"picture%d.png", self.cacheCounter];
NSURL* pictureURL = [pathURL URLByAppendingPathComponent:pictureString];
NSURL* absolutePictureURL = [pictureURL absoluteURL];
NSLog(@"%@", pictureURL);
NSData *cacheImageData = UIImagePNGRepresentation(image);
if ([cacheImageData writeToURL:pictureURL atomically:YES])
{
NSLog(@"It worked");
NSArray* cacheDirectory = [fileManager contentsOfDirectoryAtPath:[pathURL absoluteString] error:nil];
NSLog(@"%@", cacheDirectory);
}
else
{
NSLog(@"An error occurred");
}
这是我的错误日志
2013-10-03 22:36:48.920 Assignment4[14754:c07] (
"file://localhost/Users/ddelnano/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/683F512B-7FFC-4C3E-9526-715B09AABD56/Library/Caches/com.DelNano.Dom.Assignment4/",
"file://localhost/Users/ddelnano/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/683F512B-7FFC-4C3E-9526-715B09AABD56/Library/Caches/Snapshots/",
"file://localhost/Users/ddelnano/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/683F512B-7FFC-4C3E-9526-715B09AABD56/Library/Caches/stanfordpictures/"
)
file://localhost/Users/ddelnano/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/683F512B-7FFC-4C3E-9526-715B09AABD56/Library/Caches/stanfordpictures/picture0.png
2013-10-03 22:36:49.031 Assignment4[14754:c07] It worked
2013-10-03 22:36:49.032 Assignment4[14754:c07] (null)
所以基本上我NSData
writeToFile:atomically:
的返回true,但是当我检查目录“stanfordphotos”时,该NSFileManager
方法返回一个空数组。任何帮助将不胜感激。