iOS 打开相册,保存图片到相册,打开相机
2016-04-17 11:47
435 查看
在开发过程中我们经常会用到相册与相机
下面我讲一下相册与相机的基本操作:
1. 打开相册 2. 保存图片到相册 3. 打开相机
1,打开相册
//1. 要遵循代理 <UINavigationControllerDelegate, UIImagePickerControllerDelegate> //2. 自定义一个按钮 UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem]; [btn setTitle:@"打开相册" forState:UIControlStateNormal]; btn.frame = CGRectMake(100, 100, 100, 100); [btn addTarget:self action:@selector(toQRCode:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn];
//3. 执行按钮的方法 - (void)toQRCode:(UIButton *)btn { NSLog(@"打开相册"); UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.allowsEditing = true; [self presentViewController:picker animated:true completion:nil]; }
效果图:
2,保存图片到相册
// 1. // 定义个全局 UIImageView _imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]]; _imageView.frame = CGRectMake(kScreen_Width/2-100, kScreen_Height/2-70, 200, 200); [self.view addSubview:_imageView]; // 启动用户交互 _imageView.userInteractionEnabled = YES; // 手势 长按 UILongPressGestureRecognizer *longAccording = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longAccordingAction:)]; [_imageView addGestureRecognizer:longAccording];
//2. #pragma mark - 长按 执行的方法 - (void)longAccordingAction:(UILongPressGestureRecognizer *)sender { NSLog(@"长按"); if (sender.state == UIGestureRecognizerStateBegan) { // 定义个提示框 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"是否要保存到相册" message:nil preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]; [alertController addAction:action1]; UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { // 关键代码在这里 UIImageWriteToSavedPhotosAlbum(_imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL); }]; [alertController addAction:action]; [self presentViewController:alertController animated:YES completion:^{ }]; } }
//3. #pragma mark - 保存图片成功后 回调一个方法 - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo; { NSString *msg=nil; if (!error) { msg=@"保存成功"; } else { msg=@"保存失败"; } NSLog(@"%@",msg); }
效果图:
3,打开相机
//1. 要遵循 <UINavigationControllerDelegate, UIImagePickerControllerDelegate>代理 //2. 自定义一个按钮 UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem]; [btn setTitle:@"打开相机" forState:UIControlStateNormal]; btn.frame = CGRectMake(100, 100, 100, 100); [btn addTarget:self action:@selector(toQRCode:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn];
//3. 执行按钮的方法 - (void)toQRCode:(UIButton *)btn { NSLog(@"打开相机"); UIImagePickerController *picker=[[UIImagePickerController alloc] init]; picker.sourceType=UIImagePickerControllerSourceTypeCamera; picker.delegate=self; picker.allowsEditing=YES; [self presentViewController:picker animated:YES completion:nil]; }
要打开相机 只有真机才能实现
这里就不演示了 请见谅。
解决:调用系统相册、相机发现是英文状态。
第一中方法:在info.plist里面添加: Localizedresourcescan be mixed YES
第二中方法:在info.plist里面添加:Localization
native development region China
相关文章推荐
- iOS 探讨之 "Developer Tools Access 需要控制另一个进..."
- IOS编程info.list文件参数作用
- IOS编程info.list文件参数作用
- Xcode7 中创建静态库:.a 和 .framework(二)
- iOS编程info.list文件参数作用
- iOS编程info.list文件参数作用
- iOS蓝牙4.0(BLE)-后台接收数据
- iOS蓝牙4.0(BLE)-开发
- iOS开发中视图控制器ViewControllers之间的数据传递
- iOS程序启动的过程及原理
- iOS-GCD的串行队列和并行队列的任务及实现
- IOS NSURLConnection 文件上传
- IOS摇晃处理流程
- iOS调用系统闪光灯以及实时修改屏幕亮度
- iOS学习笔记32-iCloud入门
- iOS学习笔记32-iCloud入门
- ld: -pie can only be used when targeting iOS 4.2 or later clang: error: linker command failed with e
- iOS之const,static,extern简介
- iOS VFL语言的简单实用
- iOS 让约束做动画