IOS成長之路-調用照相機和相簿功能

來源:互聯網
上載者:User

開啟相機:

//先設定sourceType為相機,然後判斷相機是否可用(ipod)沒相機,不可用將sourceType設定為相片庫    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;//    if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {//        sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//    }    //sourceType = UIImagePickerControllerSourceTypeCamera; //照相機    //sourceType = UIImagePickerControllerSourceTypePhotoLibrary; //圖片庫    //sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; //儲存的相片    UIImagePickerController *picker = [[UIImagePickerController alloc] init];//初始化    picker.delegate = self;    picker.allowsEditing = YES;//設定可編輯    picker.sourceType = sourceType;    [self presentModalViewController:picker animated:YES];//進入照相介面    [picker release];

開啟相簿:(區分pad和iphone)

for iphone:

UIImagePickerController *pickerImage = [[UIImagePickerController alloc] init];    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {        pickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;        //pickerImage.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;        pickerImage.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:pickerImage.sourceType];            }    pickerImage.delegate = self;    pickerImage.allowsEditing = NO;    [self presentModalViewController:pickerImage animated:YES];    [pickerImage release];

for ipad:

UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary;    //sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; //儲存的相片    UIImagePickerController *picker = [[UIImagePickerController alloc] init];    picker.delegate = self;    picker.allowsEditing = NO;//是否允許編輯    picker.sourceType = sourceType;    /*     如果從一個導覽按鈕處呈現,使用:     presentPopoverFromBarButtonItem:permittedArrowDirections:animated:;     如果要從一個視圖出呈現,使用:     presentPopoverFromRect:inView:permittedArrowDirections:animated:          如果裝置旋轉以後,位置定位錯誤需要在父視圖控制器的下面方法裡面重新置放:     didRotateFromInterfaceOrientation:(在這個方法體裡面重新設定rect)     然後再次調用:     - (void)presentPopoverFromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated                  */    //UIPopoverController只能在ipad裝置上面使用;作用是用於顯示臨時內容,特點是總是顯示在當前視圖最前端,當單擊介面的其他地方時自動消失。    UIPopoverController *popover = [[UIPopoverController alloc]initWithContentViewController:picker];    self.imagePicker = popover;    //permittedArrowDirections 設定箭頭方向    [self.imagePicker presentPopoverFromRect:CGRectMake(0, 0, 300, 300) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];    [picker release];    [popover release];

點擊相簿中的圖片 貨照相機照完後點擊use  後觸發的方法

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary
*)info

點擊cancel 調用的方法

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

例子:http://download.csdn.net/download/like7xiaoben/4958356

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.