转载

ios中关于UIImagePickerController的一些知识总结

记得添加MobileCoreServices.framework

及导入#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";

}

*/

正文到此结束
Loading...