图片单个选择
- (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-10-31 16:13:30 发布
本文介绍了一个简单的 iOS 应用程序中实现图片选择器的方法。通过使用 UIActionSheet 和 UIImagePickerController,用户可以从相机拍摄照片或者从本地相册选取图片,并进行编辑。文章详细解释了如何配置 UIImagePickerController 的属性以适应不同来源的图片选择。
1万+

被折叠的 条评论
为什么被折叠?



