How can I take a photo with my iPhone app?

前端 未结 5 1445
甜味超标
甜味超标 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: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
    

提交回复
热议问题