UIImagePickerController概述:
UIImagePickerController 類是擷取選擇圖片和視頻的使用者介面。我們可以用這個controller選擇我們所需要的圖片和視頻。對於這個類來說比較特殊,我們不能夠任意定製,也不可以繼承產生子類。
我們在用UIImagePickerController類的時候,只需要調用用代碼實現其在什麼位置顯示和開始,當然我們之後可以選擇所需的圖片和影片,或者也可以不選擇點擊取消即可。
但是在我們調用介面之前,我們需要確認:
一:當前裝置是否支援使用UIImagePickerController,這個時候我們需要調用isSourceTypeAvailable:方法判斷。
二:查看符合的媒體類型(圖片或者視頻),這個時候我們調用availableMediaTypesForSourceType:
方法判斷。
同時使用UIImagePickerController時,我們需要UIImagePickerControllerDelegate協議代理實現一些特定時間特定的動作。在調用網路攝影機的時候我們可以選擇使用閃光燈,但是預設條件下對視頻有10分鐘的限制,需要用videoMaximumDuration屬性更改預設時間,
三:之後我們設定介面媒體的屬性
1:sourceType //從哪選取媒體
enum {
UIImagePickerControllerSourceTypePhotoLibrary,
UIImagePickerControllerSourceTypeCamera,
UIImagePickerControllerSourceTypeSavedPhotosAlbum
};
typedef NSUInteger UIImagePickerControllerSourceType;
2: mediaTypes
mediaTypes用來確定再picker裡顯示那些類型的多媒體檔案,圖片?視頻?
+ (NSArray *)availableMediaTypesForSourceType:(UIImagePickerControllerSourceType)sourceType
一共有三個可選的代理方法UIImagePickerControllerDelegate
– imagePickerController:didFinishPickingMediaWithInfo:
– imagePickerControllerDidCancel:
– imagePickerController:didFinishPickingImage:editingInfo:
-(void) pick{
[imagePicker takePicture];
}
- (IBAction)press:(id)sender {
imagePicker = [UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.showsCameraControls = NO;
UIView* view = [UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
view.backgroundColor = [UIColor redColor];
UIButton* button = [UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
button.backgroundColor = [UIColor blueColor];
[button addTarget:self action:@selector(pick) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:button];
CGRect newFrame = CGRectMake(0.0,436,320,44);
view.frame = newFrame;
imagePicker.cameraOverlayView = view;
[self presentModalViewController:imagePicker animated:YES];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
if ([info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString*)kUTTypeImage]) {
UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);