iOS 获取相册图片(拍照或者从手机相册选择)
2018-06-28 本文已影响7人
流年小书
选择相册照片或者拍照
__weak typeof(self) weakSelf = self;
TOActionSheet *actionSheet = [[TOActionSheet alloc] init];
actionSheet.contentstyle = TOActionSheetContentStyleDefault;
actionSheet.style = TOActionSheetStylekinema;
[actionSheet addButtonWithTitle:@"拍照" icon:nil tappedBlock:^{
__strong typeof(weakSelf) sself = weakSelf;
[sself makePhoto];
}];
[actionSheet addButtonWithTitle:@"从手机相册选择" icon:nil tappedBlock:^{
__strong typeof(weakSelf) sself = weakSelf;
[sself choosePicture];
}];
[actionSheet showFromView:self.certificateImgeView inView:weakSelf.view];
拍照
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:nil];
}
从相册获取照片
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];
}
代理
//获取到图片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:^{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[self compressOriginalImage:image toMaxDataSizeKBytes:200];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (image) {
[defaults setObject:[self compressOriginalImage:image toMaxDataSizeKBytes:200] forKey:@"certificateImg"];
}
[self.tableView reloadData];
}];
}
//取消获取照片
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}