UIImagePickerController和UIAlertController結合使用,uiimagepicker多選

來源:互聯網
上載者:User

UIImagePickerController和UIAlertController結合使用,uiimagepicker多選

在處理設定檔 - 頭像的時候,通常有兩個選項,一個是調用系統相機,一個是調用系統相簿。這裡要使用的就是UIImagePickerController方法。

在頭像位置的imageView添加一個手勢,或者添加一個透明的按鈕,用來實現click方法

- (IBAction)click:(id)sender{

    //建立提醒視圖

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提醒" message:nil preferredStyle:UIAlertControllerStyleActionSheet];

    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"相機" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

         //判斷裝置是否存在網路攝影機,有就調用系統相機,沒有,就提醒使用者

        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

            //建立相機

            UIImagePickerController *picker = [[UIImagePickerController alloc] init];

            //檔案由來

            picker.sourceType = UIImagePickerControllerSourceTypeCamera; //指定資料來源來自於相機

            picker.delegate  = self;// 指定代理

            picker.allowsEditing = YES; //允許編輯

            

            //模態彈出

            [self presentViewController:picker animated:YES completion:nil];

        }else{

            //沒有網路攝影機,提醒使用者 您的裝置沒有網路攝影機

            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"您的裝置沒有網路攝影機" message:nil preferredStyle:UIAlertControllerStyleAlert];

            UIAlertAction *alertAction1 = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:nil];

            [alertController addAction:alertAction1];

            [self presentViewController:alertController animated:YES completion:nil];

        }

    }];

    [alertController addAction:alertAction];

    UIAlertAction *alertAction2 = [UIAlertAction actionWithTitle:@"相簿" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

        UIImagePickerController *pickerC = [[UIImagePickerController alloc] init];

        pickerC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//指定資料來源為相簿

        pickerC.delegate = self;  //指定代理

        pickerC.allowsEditing = YES;  // 允許編輯

        [self presentViewController:pickerC animated:YES completion:nil];

    }];

    [alertController addAction:alertAction2];

    [self presentViewController:alertController animated:YES completion:nil];

}

 

//選取圖片之後執行的方法

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

    NSLog(@"%@",info);

    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    self.photoImage.image = image;

    [picker dismissViewControllerAnimated:YES completion: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.