2011-07-25 17 views
9

iPhoneアプリでは、カメラ&の写真をプログラムで写真ギャラリーに保存したいと思います。 iPhone Simulatorで可能ですか? リンクや教材を教えてください。カメラから写真を撮る方法&フォトギャラリーにプログラムで保存する方法は?

ありがとうございます。

+2

より完全な答えを掲載シミュレータでは不可能である、あなたはあなたのコードをテストするために、デバイスを使用する必要があります。 –

答えて

18

この目的では、uiimagepickercontrollerを使用します。まず、MobileCoreServices.frameworkをインポートします。そして、カメラプルアップ:、その後

if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 
     NSArray *media = [UIImagePickerController 
          availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera]; 

     if ([media containsObject:(NSString*)kUTTypeImage] == YES) { 
      UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
      picker.sourceType = UIImagePickerControllerSourceTypeCamera; 
      //picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto; 
      [picker setMediaTypes:[NSArray arrayWithObject:(NSString *)kUTTypeImage]]; 

      picker.delegate = self; 
      [self presentModalViewController:picker animated:YES]; 
      //[picker release]; 

     } 
     else { 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Unsupported!" 
                  message:@"Camera does not support photo capturing." 
                  delegate:nil 
                cancelButtonTitle:@"OK" 
                otherButtonTitles:nil]; 
      [alert show]; 
      [alert release]; 
     } 

    } 
    else { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Unavailable!" 
                 message:@"This device does not have a camera." 
                 delegate:nil 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
     [alert show]; 
     [alert release]; 
    } 

をuiimagepickercontrollerdelegateを実装することで、写真を保存し、保存機能:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
    NSLog(@"Media Info: %@", info); 
    NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType]; 

    if([mediaType isEqualToString:(NSString*)kUTTypeImage]) { 
     UIImage *photoTaken = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 

     //Save Photo to library only if it wasnt already saved i.e. its just been taken 
     if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) { 
      UIImageWriteToSavedPhotosAlbum(photoTaken, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); 
     } 


    } 

    [picker dismissModalViewControllerAnimated:YES]; 
    [picker release]; 
} 

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { 
    UIAlertView *alert; 
    //NSLog(@"Image:%@", image); 
    if (error) { 
     alert = [[UIAlertView alloc] initWithTitle:@"Error!" 
              message:[error localizedDescription] 
              delegate:nil 
           cancelButtonTitle:@"OK" 
           otherButtonTitles:nil]; 
     [alert show]; 
     [alert release]; 
    } 

} 

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { 
    [picker dismissModalViewControllerAnimated:YES]; 
} 

画像の詳細情報:didFinishSaving ....ここで見つけることができます

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIKitFunctionReference/Reference/reference.html

はまた、この記事を見ている

https://stackoverflow.com/questions/1282830/uiimagepickercontroller-uiimage-memory-and-more

+0

これは私のために正しく働いています。私のキャプチャされた画像はデバイスギャラリーに正常に保存されていますが、ギャラリーの特定のフォルダ(新しいアルバムのようなもの)に画像を保存することは可能ですか?ありがとう –

5

このデバイスを使用する必要があります。

2

私はあなたがシミュレータにカメラがないことを実感して欲しいです.....私はそれができないと思います。あなたは間違いなくデバイスを使用することができます....あなたは画像をキャプチャするためにUIImagePickerControllerを利用することができます。

0

プログラムで写真を撮るために必要な3 prerequesitesあります

  1. は、デバイスが

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
    
  2. てカメラチェックを持っている必要がありますカメラのコントロールはピッカーがUIImagePickerControllerである(非表示にする必要があります)

    picker.showsCameraControls = NO; 
    
  3. takePi UIImagePickerControllerの投稿

    [picker takePicture];

私は数分間バグしてしまったので、takePictureメソッドもビューを閉じます。

は、カメラから写真を撮るここ Other Stack answer

関連する問題