2011-11-16 62 views
1

我试图在我的应用程序中拍摄照片并将它们保存到具有两个日期的路径(以指定拍摄照片的时间)。在应用程序内拍摄照片保存

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    UIImage *image; 
    image = [info objectForKey:UIImagePickerControllerOriginalImage]; 
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); //save to photo album 
    NSString *pathString = [NSString stringWithFormat:@"Photos/%@/%@.png",timeStarted,[NSDate date]]; //create path 
    NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:pathString]; 
    //write the files! 
    [UIImagePNGRepresentation(image) writeToFile:savePath atomically:YES]; 
} 

当我检查的文件夹@"Photos/%@",timeStarted它显示为空。我究竟做错了什么?

编辑

这里是我的测试目的的新代码:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    //Create the image and save it to the photo album 
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; 
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); 

    //Create the directory and add the file name to it 
    NSString *fileName = @"Test.png"; 
    NSString *directory = [NSHomeDirectory() stringByAppendingPathComponent:@"Photos"]; 
    NSString *fullPath = [directory stringByAppendingPathComponent:fileName]; 
    NSLog(@"Full path: %@", fullPath); 

    //Save "image" to the file path 
    [UIImagePNGRepresentation(image) writeToFile:fullPath atomically:YES]; 

    //START CHECKING : Log to check if anything was saved 
    NSError *error; 
    NSFileManager *fileMgr = [NSFileManager defaultManager]; 
    NSLog(@"Photos directory: %@", [fileMgr contentsOfDirectoryAtPath:directory error:&error]); 
    //END CHECKING 

    [camera dismissModalViewControllerAnimated:YES]; 
} 

而且我NSLog写着:

[640:707] Full path: /var/mobile/Applications/3EEDBD68-5496-458D-9EF0-062746847C83/Photos/Test.png 
[640:707] Photos directory: (null) 
+0

你是否成功将该图片保存到Photo Album? – beryllium

+0

是的,它可以按照预期保存到相册中。 – Baub

回答

2

你pathString包含无效的路径。如果您的NSLog您的路径弦,它就会是这个样子

Photos/2011-11-16 20:16:16 +0000/2011-11-16 20:16:16 +0000.png 

尝试使用常规路径保存它,如Photo.png。如果这能起作用,则表明它是路径问题。在此之后,尝试使用NSDateFormatter将日期作为有效路径的字符串输出。

+0

现在试试这个。 – Baub

+0

当我列出目录的内容时仍然会收到(null)。我将它保存为'Photos/1.png',当我检查照片和/或照片/'(注意正斜杠)时,它将返回为空。 – Baub

+0

编辑显示我用来检查文件路径的代码。也许我只是检查它错了。 – Baub