2011-11-16 63 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