简体   繁体   English

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

[英]How can I take a photo with my iPhone app?

I'm writing an iPhone app with Cocoa in xcode. 我在xcode中用Cocoa编写了一个iPhone应用程序。 I can't find any tutorials or sample code that shows how to take photos with the built in camera. 我找不到任何教程或示例代码,说明如何使用内置摄像头拍照。 How do I do this? 我该怎么做呢? Where can I find good info? 我在哪里可以找到好消息?

Thanks! 谢谢!

Just Copy and paste following code into your project to get fully implemented functionality. 只需将以下代码复制并粘贴到项目中即可获得完全实现的功能。

where takePhoto and chooseFromLibrary are my own method names which will be called on button touch. 其中takePhotochooseFromLibrary是我自己的方法名称,将在按钮触摸时调用。

Make sure to reference outlets of appropriate buttons to these methods. 确保将适当按钮的出口引用到这些方法中。

   -(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.
}

Use UIImagePickerController . 使用UIImagePickerController There is a good tutorial on this here. 这里有一个很好的教程。

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

You should set the source type to UIImagePickerControllerSourceTypeCamera or UIImagePickerControllerSourceTypePhotoLibrary . 您应该将源类型设置为UIImagePickerControllerSourceTypeCameraUIImagePickerControllerSourceTypePhotoLibrary Note that these two types result in very different displays on the screen. 请注意,这两种类型会在屏幕上显示非常不同的显示。 You should test both carefully. 你应该仔细测试。 In particular, if you are nesting the UIImagePickerController inside a UINavigationController , you can end up with multiple navigation bars and other weird effects if you are not careful. 特别是,如果您将UIImagePickerController嵌套在UINavigationController ,如果您不小心,最终可能会出现多个导航栏和其他奇怪的效果。

See also this thread 见此主题

The UIImagePickerController class lets you take pictures or choose them from the photo library. UIImagePickerController类允许您拍照或从照片库中选择它们。 Specify the source type as UIImagePickerControllerSourceTypeCamera . 将源类型指定为UIImagePickerControllerSourceTypeCamera

See also this question previously asked: Access the camera with iPhone SDK 另请参阅此问题: 使用iPhone SDK访问相机

Answer posted by @WQS works fine, but contains some methods that are deprecated from iOS 6. Here is the updated answer for iOS 6 & above: @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.
}

Don't Forget to add this in your view controller.h : 不要忘记在view controller.h添加它:

@interface myVC<UINavigationControllerDelegate, UIImagePickerControllerDelegate>

Here is my code that i used to take picture for my app 这是我用来为我的应用拍照的代码

- (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];
}

if you want to retake picture just simple add this function 如果你想重拍图片只需简单添加此功能

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM