1

我一直在看这个几个小时,似乎无法弄清楚我的问题。这是我的代码

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方法返回一个空数组。任何帮助将不胜感激。

4

1 回答 1

0

CodaFi 的上述评论很棒,如果你想做同样的事情,你可以在下面这样做。虽然我已经删除了这条线。如果需要。 NSArray* cacheDirectory = [fileManager contentsOfDirectoryAtPath:[pathURL absoluteString] error:nil];这样您就可以直接将文件名作为字符串获取,而无需考虑数组,:-

    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 = @"hussain_2156.pptx";//[NSString stringWithFormat:@"picture%d.png", self.cacheCounter];
    NSURL* pictureURL = [pathURL URLByAppendingPathComponent:pictureString];
    NSLog(@"%@", pictureURL);
    NSData *cacheImageData = [NSData dataWithContentsOfFile:@"/Users/hussain/Desktop/hussain_2156.pptx"];
    if ([cacheImageData writeToURL:pictureURL atomically:YES])
    {
        NSLog(@"It worked");
        NSString *file=nil;
        NSDirectoryEnumerator *dirEnum = [fileManager enumeratorAtPath:[pathURL path]];
        while(file =[dirEnum nextObject])
        {
            NSLog(@"%@",file);
        }
    }
    else
    {

        NSLog(@"An error occurred");
    }
于 2013-10-04T05:51:57.617 回答