UIImagePickerController 视频和图片的获取

1 创建UIImagePickerController 对象

//UIImagePickerController对象
UIImagePickerController *pickerImage = [[UIImagePickerController alloc] init];
//设置资源来源
pickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//设置视频格式
pickerImage.mediaTypes = [[NSArray alloc] initWithObjects:(NSString*) kUTTypeMovie, (NSString*) kUTTypeVideo, nil];


//下面是支持的各种格式的视频你都可以添加进去。
extern const CFStringRef kUTTypeAudiovisualContent                   
extern const CFStringRef kUTTypeMovie                                
extern const CFStringRef kUTTypeVideo                                
extern const CFStringRef kUTTypeAudio                                
extern const CFStringRef kUTTypeQuickTimeMovie                       
extern const CFStringRef kUTTypeMPEG                                 
extern const CFStringRef kUTTypeMPEG2Video                           
extern const CFStringRef kUTTypeMPEG2TransportStream                 
extern const CFStringRef kUTTypeMP3                                  
extern const CFStringRef kUTTypeMPEG4                                
extern const CFStringRef kUTTypeMPEG4Audio                           
extern const CFStringRef kUTTypeAppleProtectedMPEG4Audio             
extern const CFStringRef kUTTypeAppleProtectedMPEG4Video             
extern const CFStringRef kUTTypeAVIMovie                             
extern const CFStringRef kUTTypeAudioInterchangeFileFormat           
extern const CFStringRef kUTTypeWaveformAudio                        
extern const CFStringRef kUTTypeMIDIAudio                            

2 iOS 获取图片有三种方法:

    1. 直接调用摄像头拍照
    1. 从相册中选择
    1. 从图库中选择

UIImagePickerController 是系统提供的用来获取图片和视频的接口;

用UIImagePickerController 类来获取图片视频,大体分为以下几个步骤:

    1. 初始化UIImagePickerController 类;
    1. 设置UIImagePickerController 实例的数据来源类型(下面解释);
    1. 设置设置代理;
    1. 如果需要做图片修改的话设置allowsEditing =yes。
数据来源类型一共有三种:
enum {
   UIImagePickerControllerSourceTypePhotoLibrary ,//来自图库
   UIImagePickerControllerSourceTypeCamera ,//来自相机
   UIImagePickerControllerSourceTypeSavedPhotosAlbum //来自相册
};
在用这些来源的时候最好检测以下设备是否支持;
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        NSLog(@"支持相机");
    }
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    {
        NSLog(@"支持图库");
    }
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
    {
        NSLog(@"支持相片库");
    }
调用摄像头来获取资源
- (void)viewDidLoad {
    [super viewDidLoad];
    picker = [[UIImagePickerController alloc]init];
    picker.view.backgroundColor = [UIColor orangeColor];
    UIImagePickerControllerSourceType sourcheType = UIImagePickerControllerSourceTypeCamera;
    picker.sourceType = sourcheType;
    picker.delegate = self;
    picker.allowsEditing = YES;
}
  • 上面只是实例了UIImagePickerController及其属性 在需要获取图片的时候需要弹出窗口调用
[self presentViewController:picker animated:YES completion:nil];
  • 我们还需要代理来获取我们选中的图片 UIImagePickerControllerDelegate
    代理中一共三个方法 其中一个3.0 已经废弃了,只剩下两个我们需要用的
  • 当用户选取完成后调用;选取的信息都在info中,info 是一个字典。
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary
 *)info;

当用户取消选取时调用;

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
  • 字典中的键:
SString *const  UIImagePickerControllerMediaType ;指定用户选择的媒体类型(文章最后进行扩展)
NSString *const  UIImagePickerControllerOriginalImage ;原始图片
NSString *const  UIImagePickerControllerEditedImage ;修改后的图片
NSString *const  UIImagePickerControllerCropRect ;裁剪尺寸
NSString *const  UIImagePickerControllerMediaURL ;媒体的URL
NSString *const  UIImagePickerControllerReferenceURL ;原件的URL
NSString *const  UIImagePickerControllerMediaMetadata;当来数据来源是照相机的时候这个值才有效

UIImagePickerControllerMediaType 包含着KUTTypeImage 和KUTTypeMovie
  • KUTTypeImage 包含:
const CFStringRef  kUTTypeImage ;抽象的图片类型
const CFStringRef  kUTTypeJPEG ;
const CFStringRef  kUTTypeJPEG2000 ;
const CFStringRef  kUTTypeTIFF ;
const CFStringRef  kUTTypePICT ;
const CFStringRef  kUTTypeGIF ;
const CFStringRef  kUTTypePNG ;
const CFStringRef  kUTTypeQuickTimeImage ;
const CFStringRef  kUTTypeAppleICNS 
const CFStringRef kUTTypeBMP;
const CFStringRef  kUTTypeICO;
  • KUTTypeMovie 包含:
const CFStringRef  kUTTypeAudiovisualContent ;抽象的声音视频
const CFStringRef  kUTTypeMovie ;抽象的媒体格式(声音和视频)
const CFStringRef  kUTTypeVideo ;只有视频没有声音
const CFStringRef  kUTTypeAudio ;只有声音没有视频
const CFStringRef  kUTTypeQuickTimeMovie ;
const CFStringRef  kUTTypeMPEG ;
const CFStringRef  kUTTypeMPEG4 ;
const CFStringRef  kUTTypeMP3 ;
const CFStringRef  kUTTypeMPEG4Audio ;
const CFStringRef  kUTTypeAppleProtectedMPEG4Audio;

UIImagePickerController
更多详细资料

代理中的功能参考

更多详细资料

你可能感兴趣的:(UIImagePickerController 视频和图片的获取)