Dawninest

Objective-C | 相册和相机

获取相册照片

1
2
3
4
5
6
7
8
9
- (void)selectAlbum {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
imagePicker.delegate = self;
//picker的资源类型
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//是否编辑照片
imagePicker.allowsEditing = YES;
[self presentViewController:imagePicker animated:YES completion:nil];
}

开启相机功能采集图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (void)takePhoto {
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
NSLog(@"error");
return;
}
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
imagePicker.delegate = self;
//picker的资源类型
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
//是否编辑照片
//imagePicker.allowsEditing = YES;

[self presentViewController:imagePicker animated:YES completion:nil];
}

图片获得

1
2
3
4
5
6
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
//NSLog(@"%@",info);
_imageView.image = info[@"UIImagePickerControllerEditedImage"];
[picker dismissViewControllerAnimated:YES completion:nil];//选择完后隐藏选择图片功能
}