无法在IOS 4.2中找出此文件行为

时间:2010-12-21 18:00:27

标签: ios nsfilemanager

文件行为的奇怪行为。事情就是这样:我正在使用手机摄像头拍照,并在内部生成缩略图。我将它们保存为Documents目录中的临时文件。

这是完整的代码:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

UIImage *photo = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
photo = [self scaleAndRotateImage:photo];

// save photo
NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSLog(@"Writing to %@",docsDir);

// save photo file
NSLog(@"Saving photo as %@",[NSString stringWithFormat:@"%@/temp_photo.png",docsDir]);
[UIImagePNGRepresentation(photo) writeToFile:[NSString stringWithFormat:@"%@/temp_photo.png",docsDir] atomically:YES];

// make and save thumbnail
NSLog(@"Saving thumbnail as %@",[NSString stringWithFormat:@"%@/temp_thumb.png",docsDir]);
UIImage *thumb = [self makeThumbnail:photo];
[UIImagePNGRepresentation(thumb) writeToFile:[NSString stringWithFormat:@"%@/temp_thumb.png",docsDir] atomically:YES];

NSFileManager *manager = [NSFileManager defaultManager];    
NSError *error;
NSArray *files = [manager contentsOfDirectoryAtPath:docsDir error:&error];
NSLog(@"\n\nThere are %d files in the documents directory %@",(files.count),docsDir);

for (int i = 0; i < files.count; i++) {
    NSString *filename = [files objectAtIndex:i];
    NSLog(@"Seeing filename %@",filename);
}

// done
//[photo release];
//[thumb release];
[self dismissModalViewControllerAnimated:NO];
[delegate textInputFinished];
}

正如你所看到的,我已经在这里进行了相当多的登录,试图找出我的问题(即将出现)。此时输出的日志是:

Writing to /var/mobile/Applications/77D792DC-A224-4A47-8A4C-BB7C557626F3/Documents

Saving photo as /var/mobile/Applications/77D792DC-A224-4A47-8A4C-BB7C557626F3/Documents/temp_photo.png

Saving thumbnail as /var/mobile/Applications/77D792DC-A224-4A47-8A4C-BB7C557626F3/Documents/temp_thumb.png

There are 3 files in the documents directory /var/mobile/Applications/77D792DC-A224-4A47-8A4C-BB7C557626F3/Documents

Seeing filename temp_photo.png

Seeing filename temp_text.txt

Seeing filename temp_thumb.png

这绝对是预期的。我在设备上显然有三个文件。这是下一个操作的代码 - 接收textInputFinished消息的代码:

- (void)textInputFinished {

NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSError *error;

// get next filename
NSString *filename;
int i = 0;
do {
    i++;
    filename = [NSString stringWithFormat:@"%@/reminder_%d",docsDir,i];
    NSLog(@"Trying filename %@",filename);
} while ([fileManager fileExistsAtPath:[filename stringByAppendingString:@".txt"]]);

NSLog(@"New base filename is %@",filename);

NSArray *files = [fileManager contentsOfDirectoryAtPath:docsDir error:&error];
NSLog(@"There are %d files in the directory %@",(files.count),docsDir);

这是为了获得一个新的,非临时的,不使用的文件名。它做到了 - 但它说文件夹中没有任何文件!这是记录的输出:

Trying filename /var/mobile/Applications/77D792DC-A224-4A47-8A4C-BB7C557626F3/Documents/reminder_1

New base filename is /var/mobile/Applications/77D792DC-A224-4A47-8A4C-BB7C557626F3/Documents/reminder_1

There are 0 files in the directory /var/mobile/Applications/77D792DC-A224-4A47-8A4C-BB7C557626F3/Documents

到底是什么?这三个文件在哪里?

2 个答案:

答案 0 :(得分:1)

这似乎是一个奇怪的答案,但由于我最近在UIImagePickerController上遇到了一些特殊的图像可用性问题,请尝试这样做:在选择器委托调用之后立即解除并释放它:

- (void)imagePickerController:(UIImagePickerController *)picker 
                      didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissModalViewControllerAnimated:NO];
    [picker release];

然后转到其余代码。由于我不知道的原因,info的内容直到发布后才真正可用。

如果这是你的解决方案,我仍然不明白为什么文件似乎存在然后消失。还是值得一试。

答案 1 :(得分:1)

我想知道如果获得这样的文档目录会有所不同:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];