iOS 调用拍照、选择本地相册、上传功能---未完善。
2016-03-31 16:51
766 查看
1.新建viewController 拖入一个Button,添加点击事件,使用代理方法
<UIActionSheetDelegate,UIImagePickerControllerDelegate>
2.代码如下- (IBAction)DoChoose:(id)sender {
<UIActionSheetDelegate,UIImagePickerControllerDelegate>
2.代码如下- (IBAction)DoChoose:(id)sender {
UIActionSheet *sheet; //检查是否有摄像头功能 if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"拍照",@"从相册选择", nil]; } else { sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"从相册选择", nil]; } sheet.tag=255; [sheet showInView:self.view]; } //代理方法,启用拍照或使用相册功能 -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (actionSheet.tag == 255) { NSUInteger sourceType = 0; if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { switch (buttonIndex) { case 0: return; case 1: sourceType = UIImagePickerControllerSourceTypeCamera; break; case 2: sourceType = UIImagePickerControllerSourceTypePhotoLibrary; break; default: break; } } else { if (buttonIndex ==0) { 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<NSString *,id> *)info { //返回到主界面中 [picker dismissViewControllerAnimated:YES completion:^{}]; //获取返回的图片 UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; //压缩图片 NSData *imageData = UIImageJPEGRepresentation(image, 0.5); //沙盒,准备保存的图片地址和图片名称 NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"x.jpg"]; //将图片写入文件中 [imageData writeToFile:fullPath atomically:NO]; //通过路径获取到保存的图片,可以在主界面上的image进行预览 UIImage *saveImage = [[UIImage alloc]initWithContentsOfFile:fullPath]; [self.img1 setImage:saveImage]; //将图片变为Base64格式,可以将数据通过接口传给后台 NSData *data = UIImageJPEGRepresentation(saveImage, 1.0f);
NSString *baseString = [data base64Encoding]; }
//关闭拍照窗口 -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [self dismissViewControllerAnimated:YES completion:^{}]; }
相关文章推荐
- IOS TextField伴随键盘移动
- iOS编程 手动忽略clang编译器警告
- Block - 8
- iOS 多线程 NSOperation的简单使用——iOS 编码复习(八)(多线程6)
- iOS开发过程当中使用@synchronized关键字学习文档(一)
- 便携式蓝牙打印机iOS sdk
- iOS开发中怎么样使用激光推送
- iOS中制作一张水印图片
- iOS中JSON解析三方库的比较
- ios md5加密大小写
- iOS内存管理方法
- iOS之 动态热修复技术JSPatch(二)
- 欢迎使用CSDN-markdown编辑器
- IOS-支付宝
- ios实现长时间后台运行
- iOS之动态热修复技术JSPatch(一)
- ios 单例的写法
- iOS应用崩溃日志分析 iOS应用崩溃日志揭秘
- iOS中GCD的使用小结
- IOS中NSUserDefaults的用法(轻量级本地数据存储)