无法弄清楚 IOS 4.2 中的此文件行为

发布于 2024-10-08 19:37:42 字数 3267 浏览 3 评论 0原文

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

这是完整的代码:

- (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

到底是什么?这三个文件去哪了?

Odd behavior with file behavior. Here's the thing: I'm using the phone camera to snap a picture, and internally generating a thumbnail. I'm saving those as temp files in the Documents directory.

Here's the complete code:

- (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];
}

As you can see, I've put quite a bit of logging in here in an attempt to figure out my problem (which is coming up). The log output to this point is:

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

This is absolutely as-expected. I clearly have three files on the device. Here's the very next code that operates - the code that received the textInputFinished message:

- (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);

This is testing to get a new, non-temp, not-in-use filename. It does that - but then it says there aren't any files in the documents folder! Here's the logged output:

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

What the heck? Where did the three files go?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你是年少的欢喜 2024-10-15 19:37:42

我想知道像这样获取文档目录是否会有所不同:

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

I wonder if getting the documents directory like this would make any difference:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
雨的味道风的声音 2024-10-15 19:37:42

这看起来像是一个奇怪的答案,但由于我最近在 UIImagePickerController 上遇到了一些特殊的图像可用性问题,请尝试以下操作:在选择器的委托调用之后立即关闭并释放它:

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

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

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

This will seem like a strange answer, but since I've recently had some peculiar image availability issues with the UIImagePickerController, try this: immediately after the picker's delegate call, dismiss and release it:

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

Then move on to the rest of your code. For reasons unknown to me, the contents of info are not actually available until after the release.

If this turns out to be your solution, I still wouldn't understand why the files would seem to exist and then vanish. Still, well worth a shot.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文