图片单个选择
- (void)clickHeadPicture{
UIActionSheet * action = [[UIActionSheet alloc] initWithTitle:@"选择相片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"相机" otherButtonTitles:@"本地相册", nil];
[action showInView:self.view];
}
#pragma mark - UIActionSheet代理
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 2)
{
return ;
}
//创建图片选择器
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
//设置代理
imagePicker.delegate = self;
//设置图片选择属性
imagePicker.allowsEditing = YES;
if (buttonIndex == 0) { //照相
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){//真机打开
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
}else{// 模拟器打开
NSLog(@"模拟器打开");
return;
}
}else{//相册
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
// 进去选择器
[self presentViewController:imagePicker animated:YES completion:nil];
self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
UIImage *infoImage = info[UIImagePickerControllerEditedImage];
contactImage = infoImage;
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark --图片base64
- (NSString *)imageBase64Description:(UIImage *)image
{
NSData * data = UIImageJPEGRepresentation(image, 0.2f);
NSString * encode = [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
return encode;
}
iOS 图片的选择(包括相册和相机) 并且将图片转换成base64 的字符串
最新推荐文章于 2024-07-11 14:36:42 发布