在app开发过程中,会有很多需要调取摄像头的地方,例如上传头像,上传图片等,会包含是直接拍照或者是从相册取,图片如下:
首先需要用到一个类:UIImagePickerController,这个是用来调取相机的类,在使用之前,需要先遵守UIImagePickerControllerDelegate协议和UINavigationControllerDelegate协议。
然后拍照的方法是:
UIImagePickerController* picker = [[UIImagePickerControlleralloc]init];
picker.delegate=self;
picker.allowsEditing=YES;
picker.sourceType=UIImagePickerControllerSourceTypeCamera;
[selfpresentViewController:pickeranimated:YEScompletion:^{
}];
从相册选择的方法是:
UIImagePickerController* picker = [[UIImagePickerControlleralloc]init];
picker.delegate=self;
picker.allowsEditing=YES;
picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
[selfpresentViewController:pickeranimated:YEScompletion:^{
}];
这样就可以实现调取相机的功能了