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

이 이미지를 성공적으로 사진 앨범에 저장 했습니까? – beryllium

+0

예, 의도 한대로 사진 앨범에 저장합니다. – Baub

답변

2

귀하의 pathString 잘못된 경로가 포함

여기 테스트 목적으로 나의 새로운 코드입니다. 당신이 당신의 경로 문자열을 NSLog 경우는 Photo.png로 정규 경로를 사용하여 저장하십시오이

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

처럼 보일 것입니다. 이것이 작동하면 경로에 문제가 있음을 나타냅니다. 그 후에 NSDateFormatter을 사용하여 날짜를 유효한 경로 인 문자열로 출력하십시오.

+0

지금 시도하십시오. – Baub

+0

디렉토리의 내용을 나열 할 때 여전히 (null)이됩니다. 'Photos/1.png'로 저장하고'사진'과/또는'사진 /'(슬래시에주의)을 체크하면 null로 반환됩니다. – Baub

+0

파일 경로를 확인하는 데 사용하는 코드를 표시하도록 편집되었습니다. 어쩌면 내가 잘못 확인했을 수도 있습니다. – Baub

관련 문제