iOS调用系统照相机和相册
/**
* 调用照相机
*/
- (void)opencamera
{
uiimagepickercontroller *picker = [[uiimagepickercontroller alloc] init];
picker.delegate = self;
picker.allowsediting = yes; //可编辑
//判断是否可以打开照相机
if ([uiimagepickercontroller issourcetypeavailable:uiimagepickercontrollersourcetypecamera])
{
//摄像头
picker.sourcetype = uiimagepickercontrollersourcetypecamera;
[self presentviewcontroller:picker animated:yes completion:nil];
}
else
{
nslog(@"没有摄像头");
}
}
/**
* 打开相册
*/
-(void)openphotolibrary
{
// supported orientations has no common orientation with the application, and [puuialbumlistviewcontroller shouldautorotate] is returning yes
// 进入相册
if([uiimagepickercontroller issourcetypeavailable:uiimagepickercontrollersourcetypephotolibrary])
{
uiimagepickercontroller *imagepicker = [[uiimagepickercontroller alloc]init];
imagepicker.allowsediting = yes;
imagepicker.sourcetype = uiimagepickercontrollersourcetypephotolibrary;
imagepicker.delegate = self;
[self presentviewcontroller:imagepicker animated:yes completion:^{
nslog(@"打开相册");
}];
}
else
{
nslog(@"不能打开相册");
}
}
#pragma mark - uiimagepickercontrollerdelegate
// 拍照完成回调
- (void)imagepickercontroller:(uiimagepickercontroller *)picker didfinishpickingimage:(uiimage *)image editinginfo:(nullable nsdictionary
{
nslog(@"finish..");
if(picker.sourcetype == uiimagepickercontrollersourcetypecamera)
{
//图片存入相册
uiimagewritetosavedphotosalbum(image, nil, nil, nil);
}
[self dismissviewcontrolleranimated:yes completion:nil];
}
//进入拍摄页面点击取消按钮
- (void)imagepickercontrollerdidcancel:(uiimagepickercontroller *)picker
{
[self dismissviewcontrolleranimated:yes completion:nil];
}