UIImagePickerController---图片添加---初级使用
1.创建UIImageView
-(void)creatPhotoImageView
{
self.photoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 20, 80, 80)];
self.photoImageView.backgroundColor = [UIColor blackColor];
// 打开用户交互(默认关闭)
self.photoImageView.userInteractionEnabled = YES;
[self addSubview:self.photoImageView];
}
2.在创建的UIImageView上添加轻拍手势
// 轻拍手势
UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGRAction:)];
// 添加手势
[self.rootView.photoImageView addGestureRecognizer:tapGR];
2.1手势方法---创建UIActionSheet---设置代理(遵守代理协议)
- (void)tapGRAction:(UITapGestureRecognizer *)sender
{
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"请选择" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册选取",@"拍照", nil];
[sheet showInView:self.rootView];
NSLog(@"%ld",sheet.cancelButtonIndex);
}
3.UIActionSheetDelegate协议代理方法---对应的按钮添加事件---给创建的UIImagePickerController设置代理(遵守代理协议UINavigationControllerDelegate,UIImagePickerControllerDelegate)
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"%ld",buttonIndex);
if (buttonIndex == actionSheet.firstOtherButtonIndex) {
// 检测照片源是否可用
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *imagePickerVC = [[UIImagePickerController alloc] init];
// 通过代理方法拿到图片
imagePickerVC.delegate = self;
// 编辑设置默认no 代理方法key为UIImagePickerControllerEditedImage时 必须设置为YES
imagePickerVC.allowsEditing = YES;
// 指定imagePickerVC从相册获取
imagePickerVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
// 模态一个控制器
[self presentViewController:imagePickerVC animated:YES completion:nil];
}
}else if (buttonIndex == actionSheet.firstOtherButtonIndex + 1){
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *imagePickerVC = [[UIImagePickerController alloc] init];
imagePickerVC.delegate = self;
imagePickerVC.allowsEditing = YES;
// 指定imagePickerVC从相机中获取
imagePickerVC.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imagePickerVC animated:YES completion:nil];
}
}
}
4.UINavigationControllerDelegate,UIImagePickerControllerDelegate协议方法---取消模态---取出图片---给对应的UIImageView设置图片
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// picker消失 (模态消失)
[picker dismissViewControllerAnimated:YES completion:nil];
// 根据定好的key值取出图片
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
self.rootView.photoImageView.image = image;
}
这就完成了给一个UIImageView从相册或者自拍照中添加图片
转载于:https://www.cnblogs.com/bachl/p/4649276.html
- 点赞
- 收藏
- 分享
- 文章举报
![](https://g.csdnimg.cn/static/user-reg-year/1x/4.png)
- UI控件---CollectionView
- UIScrollView约束的添加
- UITableViewCell与cell之间的那条线
- Sky TV ruling begs the question 'Why can't we let British companies succeed?'
- 认识arduino入门必备知识
- iOS-UIButton简单的使用介绍
- UITableView个人笔记
- Arduino超声波传感器测量距离
- hardware-fireduino
- arduino于RFID
- arduino与RF
- XLMHttpRequest对象的status属性,readyState属性以及onreadystatechange事件
- Aion Quest: Go Back into the Frillneck Habitat
- ValueError: Invalid configuration: - Deprecated option 'domaincontroller': use 'http_authenticator
- 深度学习入门二分类例子中ValueError: Object arrays cannot be loaded when allow_pickle=False
- [转]flash builder 4 编译器参数
- STL stack和queue的用法
- 【经验】CruiseControl.Net的安装、配置和遇到的一些问题
- 9 子线程更新UI
- 移动UI设计学习总结