如何使用我的iPhone应用程序拍照?


18

我正在xcode中编写一个可可的iPhone应用程序。我找不到任何教程或示例代码,显示如何使用内置摄像头拍摄照片。我该怎么做呢?我在哪里可以找到好的信息?

谢谢!

4

UIImagePickerController类,可以拍照或从照片库中选择它们。指定源类型为UIImagePickerControllerSourceTypeCamera

也看到这个问题以前问: Access the camera with iPhone SDK


11

使用UIImagePickerController。这里有一个很好的教程。

http://www.zimbio.com/iPhone/articles/1109/Picking+Images+iPhone+SDK+UIImagePickerController

您应在源类型设置为UIImagePickerControllerSourceTypeCameraUIImagePickerControllerSourceTypePhotoLibrary。请注意,这两种类型会在屏幕上产生非常不同的显示。你应该仔细测试。特别是,如果你是嵌套UIImagePickerController一个UINavigationController里面,你可以使用多个导航栏和其他怪异的影响结束了,如果你不小心。

又见this thread


24

复制并粘贴下面的代码到你的项目中得到充分实现的功能。

其中takePhotochooseFromLibrary是将按钮触摸来叫我自己的方法名。

确保引用的相应按钮出口到这些方法。

-(IBAction)takePhoto :(id)sender 

{ 
     UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; 

     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
     { 
      [imagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera]; 
     } 

     // image picker needs a delegate, 
     [imagePickerController setDelegate:self]; 

    // Place image picker on the screen 
    [self presentModalViewController:imagePickerController animated:YES]; 
} 



-(IBAction)chooseFromLibrary:(id)sender 
{ 

    UIImagePickerController *imagePickerController= [[UIImagePickerController alloc] init]; 
    [imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; 

    // image picker needs a delegate so we can respond to its messages 
    [imagePickerController setDelegate:self]; 

    // Place image picker on the screen 
    [self presentModalViewController:imagePickerController animated:YES]; 

} 

//delegate methode will be called after picking photo either from camera or library 
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    [self dismissModalViewControllerAnimated:YES]; 
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; 

    [myImageView setImage:image]; // "myImageView" name of any UIImageView. 
} 

1

答案张贴@WQS工作正常,但包含一些方法是从的iOS 6弃用这里是iOS 6的&更新的答案以上:

-(void)takePhoto 
{ 
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; 

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
    { 
     [imagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera]; 
    } 

    // image picker needs a delegate, 
    [imagePickerController setDelegate:self]; 

    // Place image picker on the screen 
    [self presentViewController:imagePickerController animated:YES completion:nil]; 
} 



-(void)chooseFromLibrary 
{ 

    UIImagePickerController *imagePickerController= [[UIImagePickerController alloc]init]; 
    [imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; 

    // image picker needs a delegate so we can respond to its messages 
    [imagePickerController setDelegate:self]; 

    // Place image picker on the screen 
    [self presentViewController:imagePickerController animated:YES completion:nil]; 

} 

//delegate methode will be called after picking photo either from camera or library 
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    [self dismissViewControllerAnimated:YES completion:nil]; 
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; 

[myImageView setImage:image]; // "myImageView" name of any UImageView. 
} 

不要忘记添加这在你的view controller.h

@interface myVC<UINavigationControllerDelegate, UIImagePickerControllerDelegate> 

0

这里是我的代码,我用来拍照我的应用程序

- (IBAction)takephoto:(id)sender { 

    picker = [[UIImagePickerController alloc] init]; 
    picker.delegate = self; 
    [picker setSourceType:UIImagePickerControllerSourceTypeCamera]; 
    [self presentViewController:picker animated:YES completion:NULL]; 


} 
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    img = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
    [imageview setImage:img]; 
    [self dismissViewControllerAnimated:YES completion:NULL]; 
} 

,如果你想夺回图片只是简单的添加这个功能

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
{ 
    [self dismissViewControllerAnimated:YES completion:NULL]; 
}