UIImagePickerController---图片添加---初级使用
2015-07-15 19:03
621 查看
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从相册或者自拍照中添加图片
-(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从相册或者自拍照中添加图片
相关文章推荐
- UIImagePickerController
- Building the main Guest Additions module
- 关于UIWindow的一点儿思考
- codeforces 558D Guess Your Way Out! II 规律
- UI3_CustomUITableViewCell
- iOS开发UI篇—懒加载
- UI2_QQ折叠-UITableViewController
- UI1_UITableViewSearchController
- MUI - 基于plus.downloader的图片懒加载功能,支持本地缓存
- JAVA UUID 生成
- UIButton - 常用方法总结
- 关于ADOQuery 莫名其妙的问题;
- UGUI判断鼠标或者手指是否点击在UI上
- priority_queue的用法
- MySQL Study之--MySQL下图形工具的使用(MySQL Query Browser)
- Your build settings specify a provisioning profile with the UUID, no provisioning profile was found
- error: invalid abbreviation code [25] for DIE at 0x0000003e in Assertion failed: (*offset_ptr == end_prologue_offset), function ParsePrologue, file /S
- 【多线程学习第一弹】支持value重复的双向Map
- require(),include(),require_once()和include_once()区别
- 源码推荐(7.15):重写UIFont方法 设置字体格式以及大小,可重用的水平滚动scrollView列表 极致性能优化