OC学习之道:UIImagePickerController的使用,使用UIImagePickerController调用本地摄像头,本地相册
2015-06-10 21:51
369 查看
// // RootViewController.m // InvokPhoto // // Copyright (c) 2015年 朝夕. All rights reserved. // #import "RootViewController.h" @interface RootViewController () @property (nonatomic, retain) UIImageView *imageView; @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; self.navigationItem.title = @"相册"; // 保存图片到相册 // [self saveImage]; //访问相册 [self visitPhoto]; //调用摄像头 [self camera]; } #pragma mark -- 访问相册 - (void)visitPhoto{ UIBarButtonItem *rigthBtn = [[UIBarButtonItem alloc] initWithTitle:@"访问相册" style:(UIBarButtonItemStyleDone) target:self action:@selector(selectPhoto:)]; self.navigationItem.rightBarButtonItem = rigthBtn; [rigthBtn release]; self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 350, 200, 200)]; _imageView.backgroundColor = [UIColor grayColor]; [self.view addSubview:_imageView]; [_imageView release]; } //相册调用 - (void)selectPhoto:(UIBarButtonItem *)barBtn{ UIImagePickerController *imagePick = [[UIImagePickerController alloc] init]; [self presentViewController:imagePick animated:YES completion:^{ imagePick.delegate = self; imagePick.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; }]; [imagePick release]; } #pragma mark --UIImagePickerController Delegate // 相册图片选中后调用 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ NSLog(@"%@", info);// 打印出相应的key信息 UIImage *image = [info objectForKey: UIImagePickerControllerOriginalImage]; // UIImagePickerControllerEditedImage 拍完照后可以进行编辑 self.imageView.image = image; [picker dismissViewControllerAnimated:YES completion:^{ }]; } // 取消按钮的点击事件 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{ } #pragma mark -- 保存图片到相册 - (void)saveImage{ UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)]; imageView.backgroundColor = [UIColor grayColor]; [self.view addSubview:imageView]; [imageView release]; NSString *urlString = @"http://www.huabian.com/uploadfile/2014/1118/20141118042305865.jpg"; NSURL *url = [NSURL URLWithString:urlString]; NSData *data = [NSData dataWithContentsOfURL:url]; // 主线程加载,一般不采用此方法 //把data转换成UIImage UIImage *image = [UIImage imageWithData:data]; imageView.image = image; //把image转成data NSData *imageData = UIImageJPEGRepresentation(image, 1); NSLog(@"%lu", (unsigned long)imageData.length); // 将图片保存到相册 UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL); } // 将图片保存在本地 - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{ NSLog(@"%@", error); } #pragma mark -- 调用摄像头 - (void)camera{ UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)]; button.tag = 200; button.frame = CGRectMake(80, 100, 100, 20); [button setTitle:@"摄像头" forState:(UIControlStateNormal)]; [button addTarget:self action:@selector(invokeCamera:) forControlEvents:(UIControlEventTouchUpInside)]; [self.view addSubview:button]; } // 调用摄像头 - (void)invokeCamera:(UIButton *)button{ UIImagePickerController *imagePick = [[UIImagePickerController alloc] init]; [self presentViewController:imagePick animated:YES completion:^{ // 判断是否有后置摄像头 // UIImagePickerControllerCameraDeviceFront ,为前置摄像头 BOOL iscamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]; if (!iscamera) { NSLog(@"没有摄像头"); return ; } imagePick.delegate = self; imagePick.sourceType = UIImagePickerControllerSourceTypeCamera; imagePick.allowsEditing = YES; //拍完照可以进行编辑 }]; [imagePick release]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
// // RootViewController.h // InvokPhoto // // Copyright (c) 2015年 朝夕. All rights reserved. // #import @interface RootViewController : UIViewController @end
相关文章推荐
- 单例模式:单例类的标准写法 - iOS - UI基础知识总结13
- UITableViewCellSeparatorStyleNone去掉分割线
- uitableview持续练习
- 关于druid数据连接池抛出的 connection holder is null 异常
- UIViewController中的viewDidLoad实现和调用
- 【源代码】StringBuilder和StringBuffer震源深度分析
- ios UITableView separatorColor 默认颜色
- 关于UItextView 的一些应用和将uitableview滚动到对应的位置
- js匹配表单name的值获取value
- Xcode环境变量 build Settings 设置
- 栈stack与队列queue
- 自定义cell,添加uilable,并且给lable添加tap手势,不响应的问题,对应解决方法
- Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value: 'L
- Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value: 'L
- 6.10 UITableView
- UGUI - 关于ScrollContent 和 Grid使用
- NSValue 封装成对象
- 6.9 UIScrollView
- 【Android】ContentValues的用法
- 使用DjangoUeditor将Ueditor移植到Django(BAE环境下)