iOS -调用系统相册,并且选用相片.

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

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

1. 初始化UIImagePickerController 类;
2. 设置UIImagePickerController 实例的数据来源类型(下面解释);
3. 设置设置代理;
4. 如果需要做图片修改的话设置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];
// 初始化系统相册界面的VC.
    picker = [[UIImagePickerController alloc]init];
// 设置VC的相关属性.
    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   代理方法 (用来获取选中或者取消图片)

// chose选中某张图片,内含参数info,图片的信息.(选中后调用此方法)
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary
 *)info {

在这里面我们可以拿到我们想要的图片,
然后回到原始界面dismiss该VC.

}

// 取消的时候调用此方法.
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;

// 已弃用.
- (void)imagePickerController:(UIImagePickerController *)picker
 didFinishPickingMediaWithInfo:(NSDictionary *)info;
选取的信息都在info中,info 是一个字典。

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


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;

另外可以再研究研究PhoneKit框架.详解链接:http://kayosite.com/ios-development-and-detail-of-photo-framework-part-two.html

你可能感兴趣的:(iOS -调用系统相册,并且选用相片.)