iphone - 在应用程序保存中拍照

标签 iphone objective-c ios uiimagepickercontroller

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

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

最佳答案

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

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

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

关于iphone - 在应用程序保存中拍照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8157872/

相关文章:

ios - 将一个应用划分为多个具有不同UI设计的应用并共享逻辑代码

iphone - iPhone 的 NSBitmapImageRep(或 CGImage 的直接像素访问)?

ios - 继续错误对象

ios - 为什么我们总是在tf.compat.v1.random.set_random_seed(1234)中使用seed =1234。有什么具体原因吗?

iphone - 在 iPhone 上使用 3G 而不是连接的 Wi-Fi

iphone - UIView 中的 UIImagePickerController

ios - 如何在 iOS 中推送整个 View ,包括导航栏和工具栏

objective-c - 与 'presentation clicker' (OS X) 集成

ios - 使用计时器安排通知触发

ios - 正确子类化 MKOverlayRenderer