2012-03-23 15 views
0

これは私を狂ってしまいます!UIImagePickerController - Appsドキュメントディレクトリから写真を保存して取得します

カメラのロールの中から既存のものを選択した場合でも、カメラの両方から自分のアプリケーションのドキュメントディレクトリに写真を保存することに成功しました。これを行うためのコードは次のとおりです。メモ:私はこの部分が動作していることを知っています。なぜなら、シミュレータでは、アプリケーションの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

関連する問題