How can I take a photo with my iPhone app?

前端 未结 5 1438
甜味超标
甜味超标 2021-01-31 19:39

I\'m writing an iPhone app with Cocoa in xcode. 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

相关标签:
5条回答
  • 2021-01-31 20:18

    Use UIImagePickerController. There is a good tutorial on this here.

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

    You should set the source type to UIImagePickerControllerSourceTypeCamera or UIImagePickerControllerSourceTypePhotoLibrary. 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.

    See also this thread

    0 讨论(0)
  • 2021-01-31 20:24

    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];
    }
    
    0 讨论(0)
  • 2021-01-31 20:28

    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:

    -(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 :

    @interface myVC<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
    
    0 讨论(0)
  • 2021-01-31 20:31

    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.

    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.
    }
    
    0 讨论(0)
  • 2021-01-31 20:34

    The UIImagePickerController class lets you take pictures or choose them from the photo library. Specify the source type as UIImagePickerControllerSourceTypeCamera.

    See also this question previously asked: Access the camera with iPhone SDK

    0 讨论(0)
提交回复
热议问题