ios中关于UIImagePickerController的一些知识总结
2015-10-16 22:28
555 查看
记得添加MobileCoreServices.framework
及导入#import <MobileCoreServices/MobileCoreServices.h>
及导入#import <MobileCoreServices/MobileCoreServices.h>
@interface PPViewController ()<UIActionSheetDelegate,UINavigationControllerDelegate, UIImagePickerControllerDelegate> { UIImagePickerController *_pickerController; } @property (weak, nonatomic) IBOutlet UIImageView *imageView; @end @implementation PPViewController - (void)viewDidLoad { [super viewDidLoad]; _pickerController = [[UIImagePickerController alloc] init]; _pickerController.delegate = self;//设置代理 _pickerController.allowsEditing = YES;//图片可编辑(放大缩小) } - (IBAction)choseImage:(id)sender { // 判断是否有相机 if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { //判断是否能摄像 if ([[UIImagePickerController availableMediaTypesForSourceType:_pickerController.sourceType] containsObject:(NSString *)kUTTypeMovie]) { _pickerController.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeMovie]; UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:nil otherButtonTitles:@"相册",@"相机",@"摄像机", nil]; sheet.tag = 2; [sheet showInView:self.view]; } else { UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:nil otherButtonTitles:@"相机",@"相册", nil]; sheet.tag = 1; [sheet showInView:self.view]; } } else {//模拟器 UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:nil otherButtonTitles:@"相册", nil]; sheet.tag = 3; [sheet showInView:self.view]; } } #pragma mark- UIActionSheet的代理方法 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (actionSheet.tag == 1) { switch (buttonIndex) { case 0: { _pickerController.sourceType = UIImagePickerControllerSourceTypeCamera; _pickerController.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage]; [self presentViewController:_pickerController animated:YES completion:nil]; } break; case 1: { _pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; _pickerController.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage]; [self presentViewController:_pickerController animated:YES completion:nil]; } break; default: break; } } if (actionSheet.tag == 2) { switch (buttonIndex) { case 0: { _pickerController.sourceType = UIImagePickerControllerSourceTypeCamera; _pickerController.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage]; [self presentViewController:_pickerController animated:YES completion:nil]; } break; case 1: { _pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; // _pickerController.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage]; [self presentViewController:_pickerController animated:YES completion:nil]; } break; case 2: { _pickerController.sourceType = UIImagePickerControllerSourceTypeCamera; // pickController.videoQuality = UIImagePickerControllerQualityTypeLow;//可以选择图片质量 _pickerController.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeMovie]; [self presentViewController:_pickerController animated:YES completion:nil]; } default: break; } } if (actionSheet.tag == 3) { if (buttonIndex == 0) { _pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; // _pickerController.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage]; [self presentViewController:_pickerController animated:YES completion:nil]; } } } - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { NSLog(@"========%@",info);//返回的值都在info里面 if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString *)kUTTypeMovie]) { _imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfFile:(NSString *)[[info objectForKey:UIImagePickerControllerMediaURL] path]]]; } else { _imageView.image = [info objectForKey:@"UIImagePickerControllerEditedImage"]; } [self dismissViewControllerAnimated:YES completion:nil]; } //如果这是一个modalViewController,需要dismiss 它发了个cancel消息,必须关闭它 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [self dismissViewControllerAnimated:YES completion:nil]; } /** * { UIImagePickerControllerMediaType = "public.image"; UIImagePickerControllerOriginalImage = "<UIImage: 0x7fe3a3dc2a20> size {1500, 1001} orientation 0 scale 1.000000"; UIImagePickerControllerReferenceURL = "assets-library://asset/asset.JPG?id=B6C0A21C-07C3-493D-8B44-3BA4C9981C25&ext=JPG"; } */ /** * { UIImagePickerControllerCropRect = "NSRect: {{0, 0}, {1500, 1003}}"; UIImagePickerControllerEditedImage = "<UIImage: 0x7fec5c8145b0> size {638, 426} orientation 0 scale 1.000000"; UIImagePickerControllerMediaType = "public.image"; UIImagePickerControllerOriginalImage = "<UIImage: 0x7fec5c80d4c0> size {1500, 1001} orientation 0 scale 1.000000"; UIImagePickerControllerReferenceURL = "assets-library://asset/asset.JPG?id=B6C0A21C-07C3-493D-8B44-3BA4C9981C25&ext=JPG"; } */
相关文章推荐
- iOS开发——高级篇——流水布局UICollectionViewFlowLayout的基本使用
- String.valueOf() 方法的使用
- UVa 10534 Wavio Sequence
- FastGUI教程
- uImage与zImage的区别
- UIKeyBinding
- POJ——3061 Subsequence
- Unique Paths II
- Unique Paths
- POJ 2533 Longest Ordered Subsequence
- Quirks模式是什么?
- Qt的Script、Quick、QML的关系与总结
- NGUI实现技能冷却
- HDU 4010 Query on The Trees
- Scala学习笔记25【ListBuffer、ArrayBuffer、Queue、Stack常用操作实战】
- 小技巧 关于UI控件
- UI - presentViewControllerAndSingleton
- ZOJ 3911 Prime Query ZOJ Monthly, October 2015 - I
- UITextField 弹出键盘时遮盖住textField的问题
- js模块化编程 : require.js