废话不多说哦,边上代码边解释咯
首先我们来讲调用系统的相机咯
UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.allowsEditing = NO; picker.sourceType = UIImagePickerControllerSourceTypeCamera; [self presentViewController:picker animated:YES completion:nil];
那么这个时候,我们还需要判断一下相关的权限,因为我们可以在手机的设置里面去更改当前app是否允许打开相机等操作
if(authStatus == ALAuthorizationStatusRestricted || authStatus == ALAuthorizationStatusDenied){ UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:NTC_CAMERA_ALERT_MESSAGE delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil]; [alertView show]; }
接下来我们会走两个代理方法,就是下面这两个了,但是要导入2个头文件的Delegate哦
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [picker dismissViewControllerAnimated:YES completion:nil]; } - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; }
第一个方法就是取消,这个没什么好说的
第二个方法是拍照完成后,点击使用照片后调用的方法
这个时候我们需要在里面取出图片,然后做相应的操作
这就是相机的简单使用咯
接下来是相册
UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.allowsEditing = NO; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [self presentViewController:picker animated:YES completion:nil];
其实跟相机的调用都差不多,只不过改一个属性而已
当然了,我们也还是要判断一下权限的哦
ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus]; if(author == ALAuthorizationStatusDenied || author == ALAuthorizationStatusRestricted) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:NTC_PHOTOS_ALERT_MESSAGE delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil]; [alertView show]; } else { //可以使用 }
然后还是走的之前的两个Delegate方法
好啦,这就是相册与拍照的最基本的使用了哦
时间: 2024-11-24 01:33:52