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

时间:2008-09-23 20:05:35

标签: iphone cocoa-touch camera

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

谢谢!

5 个答案:

答案 0 :(得分:24)

只需将以下代码复制并粘贴到项目中即可获得完全实现的功能。

其中 takePhoto chooseFromLibrary 是我自己的方法名称,将在按键触摸时调用。

确保将适当按钮的插座引用到这些方法中。

   -(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 :(得分:11)

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

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

您应将源类型设置为UIImagePickerControllerSourceTypeCameraUIImagePickerControllerSourceTypePhotoLibrary。请注意,这两种类型会在屏幕上显示非常不同的显示。你应该仔细测试。特别是,如果您将UIImagePickerController嵌套在UINavigationController内,如果不小心,最终可能会出现多个导航栏和其他奇怪的效果。

另见this thread

答案 2 :(得分:4)

UIImagePickerController课程可让您拍照或从照片库中选择照片。将源类型指定为UIImagePickerControllerSourceTypeCamera

另见前面提到的这个问题: Access the camera with iPhone SDK

答案 3 :(得分: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>

答案 4 :(得分: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];
}