【问题标题】:Take Photos Within Application Saving在应用程序保存中拍照
【发布时间】:2011-12-30 18:25:55
【问题描述】:

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

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

【问题讨论】:

  • 您成功将这张图片保存到相册了吗?
  • 是的,它按预期保存到相册中。

标签: iphone objective-c ios uiimagepickercontroller


【解决方案1】:

您的 pathString 包含无效路径。如果你 NSLog 你的路径字符串,它看起来像这样

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

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

【讨论】:

  • 当我列出目录的内容时仍然得到(null)。我将它保存为Photos/1.png,当我检查Photos 和/或Photos/(注意正斜杠)时,它返回为null。
  • 已编辑以显示我用来检查文件路径的代码。也许我只是检查错了。
  • 不要使用斜杠,它用作目录分隔符。如果要创建目录,请使用 NSFileManager 上的 createDirectoryAtPath:withIntermediateDirectories: 方法
  • 您需要先创建照片目录,然后才能将文件保存到该目录。
  • 在尝试并记录错误(以及大量谷歌搜索)后,我发现在我的应用程序签名之前,我无法在三个文件夹之外创建文件夹。在此处查看接受的答案:stackoverflow.com/questions/3439408/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-19
  • 1970-01-01
  • 1970-01-01
  • 2013-06-25
  • 1970-01-01
相关资源
最近更新 更多