-(void)tapGestureRecognizer:(UITapGestureRecognizer *)sender {
UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@"选择照片" delegate: self cancelButtonTitle:@"取消" destructiveButtonTitle:@"相机" otherButtonTitles:@"本地相册", nil];
[action showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 2) {
return;
}
//创建图片选择器
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
//设置图片选择属性
imagePicker.allowsEditing = NO;
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 {
NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
//当选择的类型是图片
if ([type isEqualToString:@"public.image"])
{
//先把图片转成NSData
UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSData *data;
if (UIImagePNGRepresentation(image) == nil)
{
data = UIImageJPEGRepresentation(image, 1.0);
}
else
{
data = UIImagePNGRepresentation(image);
}
self.headImg.image = image;
//图片保存的路径
//这里将图片放在沙盒的documents文件夹中
NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
//文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
//把刚刚图片转换的data对象拷贝至沙盒中 并保存为image.png
[fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
[fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.png"] contents:data attributes:nil];
//得到选择后沙盒中图片的完整路径
self.filePath = [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath, @"/image.png"];
//关闭相册界面
[picker dismissViewControllerAnimated:YES completion:nil];
//[self saveChangeData];
}
}
//#pragma mark - delegate
//- (void)saveChangeData {
//NSData *imagData =UIImagePNGRepresentation(self.headImg.image);
//NSString *imageStr = [[NSString alloc] initWithData:imagData encoding:NSUTF8StringEncoding];
//NSLog(@"0------%@",imageStr);
//[DataManager getInstance].user.head_image = imageStr;
//}
调用系统的相机上传照片
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 大家可以通过产看WebViewClient这个类里面的源码可知,在下面的几个方法里面5.0以下是通过ValueCa...
- 一般上传图片的步骤: 1:创建UIActionSheet,利用其代理方法判断是调用相机还是相册2:创建相机方法;3...
- 解决办法: (得到的bmpOk就是正常的图片) int degree = ImageUtil.readPictur...