2012-03-23 6 views
0

이것은 나를 미치게합니다!UIImagePickerController - 앱 문서 디렉토리에서 사진 저장 및 검색

카메라 롤과 카메라 롤에서 기존 응용 프로그램을 선택하면 내 응용 프로그램 문서 디렉토리에 성공적으로 사진을 저장하고 있습니다. 이렇게하는 코드는 다음과 같습니다. 참고 시뮬레이터에서 응용 프로그램 Documents 폴더를 탐색하고 저장할 파일을 볼 수 있기 때문에이 부분이 작동하고 있음을 알았습니다.

예 "저장"코드 : 이제

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    NSLog(@"picker returned successfully"); 

    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType]; 
    if([mediaType isEqualToString:(__bridge NSString *)kUTTypeImage]) 
    { 
     UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage]; 
     UIImage *resizedImage = [util_ createThumbnailForImage:originalImage thumbnailSize:[util_ determineIPhoneScreenSize]]; 
     NSData *imageData = UIImagePNGRepresentation(resizedImage); 
     NSString* imageName = @"MyImage.png"; 
     NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString* documentsDirectory = [paths objectAtIndex:0]; 
     NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];   
     [imageData writeToFile:fullPathToFile atomically:NO]; 

     NSLog(@"fullPathToFile %@", fullPathToFile); 
     // this outputs the following path in the debugger 
     // fullPathToFile /Users/me/Library/Application Support/iPhone Simulator/5.0/Applications/47B01A4C-C54F-45C4-91A3-C4D7FF9F95CA/Documents/MyImage.png 

     // rest snipped out - at this point I see the image in the simulators/app/Documents directory 

- 작업 (가져 와서 사진을 표시)되지 않은 부분 :

//Note: code above snipped to keep this part of the question short 
    case 1: 
    { 
     // select from library 
     NSLog(@"select from camera roll"); 

     if([util_ isPhotoLibraryAvailable]) 
     { 
      UIImagePickerController *controller = [[UIImagePickerController alloc] init]; 
      controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
      NSMutableArray *mediaTypes = [[NSMutableArray alloc] init]; 
      if([util_ canUserPickPhotosFromPhotoLibrary]) 
      { 
       [mediaTypes addObject:(__bridge NSString *)kUTTypeImage]; 
      } 
      controller.mediaTypes = mediaTypes; 
      controller.delegate = self; 
      controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
      [self presentModalViewController:controller animated:YES]; 
     } 
    } break; 
//code below snipped out 

그리고 이미지가 촬영 또는 선택되면

// code above snipped out (we're in tableView:cellForRowAtIndexPath) 

imageButton_ = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 200, 200)]; 
NSString* imageName = [contentArray_ objectAtIndex:indexPath.row]; 
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
SString* documentsDirectory = [paths objectAtIndex:0]; 
NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName]; 
UIImage *image = [UIImage imageNamed:fullPathToFile]; 
[imageButton_ setImage:image forState:UIControlStateNormal]; 

[cell addSubview:imageButton_]; 

NSLog(@"fullPathToFile: %@", fullPathToFile); 
// this outputs the following path in the debugger 
// fullPathToFile: /Users/me/Library/Application Support/iPhone Simulator/5.0/Applications/47B01A4C-C54F-45C4-91A3-C4D7FF9F95CA/Documents/MyImage.png 

return cell; 

}

그래서 셀에 이미지가 표시되지 않은 빈 버튼이 표시됩니다. 또한

+0

안녕하세요. 귀하의 질문을 읽으 셨습니다. - util_이며 귀하 께서 저에게 묻고 싶지만 util_로 선언 된 것은 무엇입니까? –

+0

util_는 내가 만든 유틸리티 클래스이며 유틸리티 유형의 메소드가 여러 개 있습니다. 특별한 것은 아닙니다. – ElasticThoughts

답변

4

귀하의 문제는 여기에 ...있는 UIImageView 여전히 행운의 버튼을 대체 한 :

UIImage *image = [UIImage imageNamed:fullPathToFile]; 

UIImage imageNamed:은 번들의 이미지입니다. 문서 디렉토리의 이미지에서는 작동하지 않습니다.

대신 imageWithContentsOfFile:을 시도하십시오.

+0

아! 그래서 다른 곳에 저장하거나 문서에 보관하고 다른 방법을 사용하여 가져와야합니까? 그리고 다른 방법은 무엇입니까? – ElasticThoughts

+0

내 편집보기 ...'imageWithContentsOfFile : ' – amattn

+0

그 트릭을 했어, 고마워! – ElasticThoughts

관련 문제