iOS相册图片的选取与设置
2015-11-17 16:36
525 查看
我的模块中常常用户的头像可以用户自定义自己的头像,可以选择拍照也可以选择从用户的自己的相册中选择,具体的实现代码如下:
@interface PickerViewController : UIViewController<UIImagePickerControllerDelegate,UIActionSheetDelegate,UINavigationControllerDelegate>
//名字 @property (weak, nonatomic) IBOutlet UILabel *namelabel; //头像 @property (weak, nonatomic) IBOutlet UIImageView *iconView; //actionsheet底部弹窗 @property (nonatomic,strong) UIActionSheet *actionSheet;
- (void)viewDidLoad { [super viewDidLoad]; self.namelabel.text = @"XXX"; self.iconView.image = [UIImage imageNamed:@"IMG_1743"]; self.iconView.layer.masksToBounds = YES; self.iconView.layer.cornerRadius = 40; self.iconView.userInteractionEnabled = YES; //给头像添加点击手势 UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick)]; [self.iconView addGestureRecognizer:ges];
- (void)tapClick { NSLog(@"打开相册"); if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从相册选取", nil]; }else { self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancelButtonTitle:@"取消"destructiveButtonTitle:nil otherButtonTitles:@"从相册选择",nil]; } self.actionSheet.tag = 1000; [self.actionSheet showInView:self.view]; } - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (actionSheet.tag == 1000) { NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary; // 判断是否支持相机 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { switch (buttonIndex) { case 0: //来源:相机 sourceType = UIImagePickerControllerSourceTypeCamera; break; case 1: //来源:相册 sourceType = UIImagePickerControllerSourceTypePhotoLibrary; break; case 2: return; } } else { if (buttonIndex == 2) { return; } else { sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; } } // 跳转到相机或相册页面 UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.delegate = self; imagePickerController.allowsEditing = YES; imagePickerController.sourceType = sourceType; [self presentViewController:imagePickerController animated:YES completion:^{ }]; } } - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [picker dismissViewControllerAnimated:YES completion:^{ }]; UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; self.iconView.image = image; // self.namelabel.text = image.description; }
相关文章推荐
- iOS_一些简单地功能代码
- ios 滑动返回出现问题以及解决方法
- iOS播放gif图
- iOS开发页面传值方案
- 我是怎样转IOS应用开发的
- 关于Git
- ShareSDK适配iOS 9系统
- ios 截图,并使用指定的区域裁剪
- JSPatch库, 一个Apple官方支持的实现在线更新iOS应用的库
- Xcode制作动态及静态Framework
- iOS巅峰之NSRunLoop
- iOS下的后台任务实现方法
- 使用BIOS中断和汉字库显示汉字
- iOS 公司开发者账号申请
- iOS开发内购全套图文教程
- iOS:JSON格式字符串转字典,字典转JSON格式字符串
- ios:从系统相册选取视频并获得视频的缩略图
- Foundation框架的简单介绍
- iOS XCode中的手机模拟器 利用键盘鼠标模拟各种手势 解决捏合手势中心不动的问题
- 【iOS开发】数据存储的四种方式