Can't figure out this file behavior in IOS 4.2

Posted by Don Jones on Stack Overflow See other posts from Stack Overflow or by Don Jones
Published on 2010-12-21T18:00:26Z Indexed on 2010/12/29 23:54 UTC
Read the original article Hit count: 220

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about ios

Related posts about nsfilemanager