iOS之Camera的使用

1.先看一下camera的一些属性

当然先要导入头文件#import

- (void)testProperty {
    if ([self isCameraAvail]) {
        NSLog(@"当前相机可用");
    }else {
        NSLog(@"当前相机不可用");
    }
    
    if ([self cameraSupportMedia:(__bridge NSString *)kUTTypeImage]) {
        NSLog(@"支持拍照");
    }else {
        NSLog(@"不支持拍照");
    }
    if ([self cameraSupportMedia:(__bridge NSString *)kUTTypeMovie]) {
        NSLog(@"支持录像");
    }else {
        NSLog(@"不支持录像");
    }
}
//检查相机是否可用
- (BOOL)isCameraAvail {
    return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
}
//检查前置摄像头闪光灯是否可用
- (BOOL)isCameraFrontFlashAvail {
    return [UIImagePickerController isFlashAvailableForCameraDevice:UIImagePickerControllerCameraDeviceFront];
    
}
//检查后置摄像头闪光灯是否可用
- (BOOL)isCameraRearFlashAvail {
    return [UIImagePickerController isFlashAvailableForCameraDevice:UIImagePickerControllerCameraDeviceRear];
    
}
//检查前置摄像头是否可用
- (BOOL)isFrontCameraAvail {
    return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
}
//检查后置摄像头是否可用
- (BOOL)isRearCameraAvail {
    return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
}
//检查支持什么媒体类型
- (BOOL)cameraSupportMedia:(NSString *)paraMediaType {
    NSArray * avaiableMedia = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
    for (NSString * item in avaiableMedia) {
        if ([item isEqualToString:paraMediaType]) {
            return true;
        }
    }
    return false;
}

2.用照相机进行拍照

在实际项目中的使用时,首先我们要检查相机的可用性,再配置UIImagePickerController

UIImagePickerController * controller = [[UIImagePickerController alloc] init];
    controller.sourceType = UIImagePickerControllerSourceTypeCamera;//配置媒体类型
    NSString * requierModieType = (__bridge NSString *)kUTTypeImage;
    controller.mediaTypes = [[NSArray alloc] initWithObjects:requierModieType, nil];
    controller.allowsEditing = false;
    controller.delegate = self;//设置代理
    //配置闪光灯
    controller.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn;
    //配置摄像头
    //controller.cameraDevice = UIImagePickerControllerCameraDeviceFront;
    [self.navigationController presentViewController:controller animated:YES completion:nil];

再需要实现UIImagePickerControllerDelegate的代理方法

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    //info拿到我们拍照的内容
    NSDictionary * dict = [info objectForKey:UIImagePickerControllerMediaMetadata];
    NSLog(@"%@", dict);//从info可以取得很多信息
    NSString * mediaType = [info objectForKey:UIImagePickerControllerMediaType];//拿到当前拍摄的媒体类型
    if ([mediaType isEqualToString:(__bridge NSString *)kUTTypeImage]) {
        UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];//拿到拍摄的照片
        self.imageView.image = image;
        self.imageView.contentMode = UIViewContentModeScaleToFill;
        
        //照片的保存
        SEL saveImage = @selector(ImageWasSavaSuccessFully:didFinishSavingWithError:contextInfo:);
        UIImageWriteToSavedPhotosAlbum(image, self, saveImage, nil);
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)ImageWasSavaSuccessFully:(UIImage *)paraimage
        didFinishSavingWithError:(NSError *)paraError
                     contextInfo:(void *)paraInfo {
    if (paraError == nil) {
        NSLog(@"图片保存成功");
    }else{
        NSLog(@"图片保存失败--%@", paraError);
    }
    
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];
}

3.从图片库选照片

UIImagePickerController * controller = [[UIImagePickerController alloc] init];
    controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    NSString * requierModieType = (__bridge NSString *)kUTTypeImage;
    controller.mediaTypes = [[NSArray alloc] initWithObjects:requierModieType, nil];
    controller.delegate = self;
    [self.navigationController presentViewController:controller animated:YES completion:nil];

你可能感兴趣的:(iOS之Camera的使用)