欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS调用系统照相机和相册

程序员文章站 2022-04-12 22:02:38
ios 10 以后记得在plist里面添加获取拍照和相册权限,ios 10 以后记得在plist里面添加获取拍照和相册权限,ios 10 以后记得在plist里面添加获取拍照和相册权限,ios 10...
ios 10 以后记得在plist里面添加获取拍照和相册权限,ios 10 以后记得在plist里面添加获取拍照和相册权限,ios 10 以后记得在plist里面添加获取拍照和相册权限,ios 10 以后记得在plist里面添加获取拍照和相册权限,ios 10 以后记得在plist里面添加获取拍照和相册权限,ios 10 以后记得在plist里面添加获取拍照和相册权限,ios 10 以后记得在plist里面添加获取拍照和相册权限,ios 10 以后记得在plist里面添加获取拍照和相册权限,ios 10 以后记得在plist里面添加获取拍照和相册权限,ios 10 以后记得在plist里面添加获取拍照和相册权限,ios 10 以后记得在plist里面添加获取拍照和相册权限,ios 10 以后记得在plist里面添加获取拍照和相册权限,ios 10 以后记得在plist里面添加获取拍照和相册权限,ios 10 以后记得在plist里面添加获取拍照和相册权限。

/**

* 调用照相机

*/

- (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 *)editinginfo ns_deprecated_ios(2_0, 3_0)

{

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];

}