iOS 獲取圖片有三種方法
1 直接調用攝像頭拍照
2 從相冊中選擇
3 從圖庫中選擇
UIImagePickerController 是系統提供的用來獲取圖片和視頻的接口;
用UIImagePickerController 類來獲取圖片視頻;
大體分為以下幾個步驟:
初始化UIImagePickerController 類設置UIImagePickerController 實例的數據來源類型(下面解釋);設置設置代理如果需要做圖片修改的話設置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; }
[self presentViewController:picker animated:YES completion:nil];
UIImagePickerControllerDelegate
代理中一共三個方法 其中一個3.0 已經廢棄了,只剩下兩個我們需要用的- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
當用戶選取完成後調用;- (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 的更多參數參考http://blog.sina.com.cn/s/blog_68edaff101019ppe.html
代理中得功能參考 http://bbs.9ria.com/forum.php?mod=viewthread&tid=244453
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;
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;