gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 23:18:27 24 4
gpt4 key购买 nike

文件行为的奇怪行为。事情是这样的:我正在使用手机摄像头拍摄照片,并在内部生成缩略图。我将它们作为临时文件保存在 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

什么鬼?三个文件去哪了?

最佳答案

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

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

关于ios - 无法弄清楚 IOS 4.2 中的此文件行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4502396/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com