通过UIImagePickerController完成照相和相片的选取
UIImagePickerController是用于选取现有照片,或者用照相机现场照一张相片使用的
定义:
@interface ShowViewController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
UIImagePickerController * imagePicker;
这里需要注意,Delegate可以不写,但是不写会有警告发生,所以还是写上比较好
使用:
imagePicker = [[UIImagePickerControlleralloc]init];
UIImagePickerControllerSourceTypesoureType;
//soureType当改成UIImagePickerControllerSourceTypeCamera时为照相机照照片模式,UIImagePickerControllerSourceTypePhotoLibrary为相册选取模式
soureType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.delegate =self;
imagePicker.allowsEditing = YES;
imagePicker.sourceType = soureType;
UIDevice *device = [UIDevice currentDevice];
NSLog(@"device.model %@",device.model);
[selfpresentViewController:imagePickeranimated:YEScompletion:^(void){
}];
现在我使用的总共是两种模式,两种模式的切换全都在SoureType里面
最后一句presentViewController用的是切换视图,把当前视图切换到选取照片的视图或者拍照片的视图。
必须实现的接口方法:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
inputImage = [info objectForKey:UIImagePickerControllerEditedImage];
[picker dismissViewControllerAnimated:YEScompletion:^(void){
}];
[appDelegatesetInputImage:inputImage];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YEScompletion:^(void){
}];
}
第一个方法是在选取照片以后,对照片的存储。通过info找到照片,并放到需要的地方。然后一句话是退出当前视图dismissViewController
第二个方法是取消照片选取以后的方法,这里我不需要进行任何动作,只需要退出当前视图就够了