IOS開發之相簿拾取器UIImagePickerController

來源:互聯網
上載者:User

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);

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.