18

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

谢谢!

4

5 回答 5

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.
}
于 2012-09-12T05:41:57.547 回答
11

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

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

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

另请参阅此线程

于 2008-09-24T13:35:03.370 回答
4

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

另请参阅之前提出的这个问题: Access the camera with iPhone SDK

于 2008-09-23T20:17:22.880 回答
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>
于 2014-02-25T12:15:51.853 回答
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];
}
于 2014-10-07T20:03:16.883 回答