ios--摄像头使用--UIImagePickerController

IOS有两种拍照与拍摄视频的方式

1.  直接使用UIImagePickerController, 这可以提供一个简单便捷的拍照与选择图库里图片的功能.

2.另一种方式就是通过AV Foundation framework,来完全自定义拍照的界面与选择图库的界面.

这里介绍第一种, 如何使用UIImagePickerController.

一.拍照

步骤

1. 定义UIImagePickerController, 启动拍照界面

(1)首先摄像头对于你的应用是必须的,那你应该在项目的Info.plist的Required device capabilities里添加自己要的设备.这里有相关的设备描述.

(2) 要确保你的设备可用,所以在代码中最好先用UIImagePickerController的isSourceTypeAvailable:方面来判断一下,否则如果直接写逻辑,那在设备不可用时,程序会抛异常.

 (3)  在拍摄完后,想要能够关掉拍摄界面,那就必须要实现UIImagePickerControllerDelegate

UIImagePickerController 里有几个比较重要的属性.

 Source type:  这个参数是用来确定是调用摄像头还是调用图片库.如果是 UIImagePickerControllerSourceTypeCamera 就是调用摄像头,如果是UIImagePickerControllerSourceTypePhotoLibrary 就是调用图片库,如果是UIImagePickerControllerSourceTypeSavedPhotosAlbum 则调用iOS设备中的胶卷相机的图片.

Media types:在拍照时,用来指定是拍静态的图片还是录像.kUTTypeImage 表示静态图片, kUTTypeMovie表示录像.

Editing controls :用来指定是否可编辑.将allowsEditing 属性设置为YES表示可编辑,NO表示不可编辑.

示例代码:(来源于apple官方文档)

 

复制代码
- (BOOL) startCameraControllerFromViewController: (UIViewController*) controller
                                   usingDelegate: (id <UIImagePickerControllerDelegate,
                                           UINavigationControllerDelegate>) delegate 
{

    // here, check the device is available  or not
    if (([UIImagePickerController isSourceTypeAvailable:
                UIImagePickerControllerSourceTypeCamera] == NO)
            || (delegate == nil)|| (controller == nil))
        return NO;

    UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];

    cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;

    // Displays a control that allows the user to choose picture or
    // movie capture, if both are available:

    cameraUI.mediaTypes = [UIImagePickerControlleravailableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];

    // Hides the controls for moving & scaling pictures, or for
    // trimming movies. To instead show the controls, use YES.
    cameraUI.allowsEditing = NO;
    cameraUI.delegate = delegate;

    [controller presentModalViewController: cameraUI animated: YES];

    returnYES;
}
复制代码

 

如果只想让摄像头只能摄像,而不能拍照,那应该设置mediaTypes

cameraUI.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *)  kUTTypeMovie  , nil];
 kUTTypeImage 对应拍照
 kUTTypeMovie  对应摄像
这要导入MobileCoreServices.framework,然后再

#import 

然后就可以在一个触发事件中进行调用.

- (IBAction) showCameraUI {
    [self startCameraControllerFromViewController: self
                                    usingDelegate: self];
}

 

2. 设置delegate

在拍摄完成后,要进行delegate的设置,这样才能够让程序互动起来. 

  UIImagePickerControllerDelegate在方法主要有两个,
- (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker ; 点击cancel后执行的方法
- (void) imagePickerController: (UIImagePickerController *) picker
            didFinishPickingMediaWithInfo: (NSDictionary *) info; 拍摄完成后要执行的方法
 
code:
 
复制代码
// For responding to the user tapping Cancel.
- (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker {
 
    [[picker parentViewController] dismissModalViewControllerAnimated: YES];
    [picker release];
}
 
 
 
 
// For responding to the user accepting a newly-captured picture or movie
- (void) imagePickerController: (UIImagePickerController *) picker
            didFinishPickingMediaWithInfo: (NSDictionary *) info {
 
    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
    UIImage *originalImage, *editedImage, *imageToSave;
 
    // Handle a still image capture
    if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0)
            == kCFCompareEqualTo) {
 
        editedImage = (UIImage *) [info objectForKey:
                    UIImagePickerControllerEditedImage];
        originalImage = (UIImage *) [info objectForKey:
                    UIImagePickerControllerOriginalImage];
 
        if (editedImage) {
            imageToSave = editedImage;
        } else {
            imageToSave = originalImage;
        }
 
    // Save the new image (original or edited) to the Camera Roll
        UIImageWriteToSavedPhotosAlbum (imageToSave, nil, nil , nil);
    }
 
    // Handle a movie capture
    if (CFStringCompare ((CFStringRef) mediaType, kUTTypeMovie, 0)
            == kCFCompareEqualTo) {
 
        NSString *moviePath = [[info objectForKey:
                    UIImagePickerControllerMediaURL] path];
 
        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath)) {
            UISaveVideoAtPathToSavedPhotosAlbum (
                    moviePath, nil, nil, nil);
        }
    }
 
    [[picker parentViewController] dismissModalViewControllerAnimated: YES];
    [picker release];
}
复制代码

 

// 这个方法就是在拍摄完成后,将图片或视频,保存到胶卷相机. 我们所需要的东西都保存在info 这个 NSDictionary中
 
 
二. 从图片库中选择图片
在选择图片中,ipad 与iphone 会有些不同, ipad展示图片展时,必须要以popover的形式展示,如果想打开一个页面,来modal展示的话,会抛出异常.
这里要设置的属性只要是将
source type改为 UIImagePickerControllerSourceTypePhotoLibrary 或UIImagePickerControllerSourceTypeSavedPhotosAlbum,这个是根据自己选择来定.
mediatype的设置与上面的拍照一样.
代码:
如果是iPhone,则代码与上面拍照的一样,只要改下source type就可以了.但如果是ipad,会有所不同,下面的代码就是ipad为例.
在interface中:

@property (strong,nonatomicUIPopoverController * popoverViewControl;

在implement中:

复制代码
- (IBAction)takeImage:(id)sender {
    if ([self.popoverViewControl isPopoverVisible])
    {
        [self.popoverViewControl dismissPopoverAnimated:YES];
    }else {
        if ([UIImagePickerController isSourceTypeAvailable:
             UIImagePickerControllerSourceTypeSavedPhotosAlbum])
        {
            UIImagePickerController * picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            picker.allowsEditing = NO;
            picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
          //  picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
            picker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)  kUTTypeMovie , nil];
            self.popoverViewControl = [[UIPopoverControlleralloc] initWithContentViewController:picker];
            self.popoverViewControl.delegate = self;
            
            CGRect popoverRect = [self.view convertRect:[sender frame] 
                                               fromView:[sender superview]];  
            popoverRect.size.width = MIN(popoverRect.size.width, 100);
            [self.popoverViewControl presentPopoverFromRect:popoverRect inView:self.view             permittedArrowDirections:UIPopoverArrowDirectionAnyanimated:YES];
        }
     }
}
 
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    self.imageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];    //imageview是自己定义的一个image view 控件,用来显示选中的图片
    [self.popoverViewControldismissPopoverAnimated:YES];
}
复制代码

 

以上就是UIImagePickerController的大概用法,主要就是自己看了apple的官方文档,然后实践后,记录下来,留用,防止忘了.

你可能感兴趣的:(ios开发,ios,摄像头)