ios 照相機(jī)和相冊(cè)的調(diào)用
一個(gè)簡(jiǎn)單的功能,上傳照片或者拍照可以用到.
//首先遵循兩個(gè)代理 <UIImagePickerControllerDelegate,UINavigationControllerDelegate> //我們創(chuàng)建一個(gè)btn和一個(gè)imageview,btn用來(lái)觸發(fā)事件調(diào)起照相機(jī)和相冊(cè)的功能,imageview用來(lái)展示選取或者拍攝的圖片. self.view.backgroundColor = [UIColor whiteColor]; UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100 , 100, 200,50)]; btn.backgroundColor = [UIColor blackColor]; [btn setTitle:@'ChoosePhoto' forState:UIControlStateNormal]; [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; _imageview = [[UIImageView alloc]initWithFrame:CGRectMake(200, 200, 100, 100)]; _imageview.backgroundColor = [UIColor lightGrayColor]; _imageview.layer.cornerRadius = CGRectGetHeight(_imageview.bounds)/2; _imageview.clipsToBounds = YES; [self.view addSubview:_imageview]; //btn的點(diǎn)擊事件 - (void)btnClick:(UIButton *)btn{ //創(chuàng)建UIAlertController是為了讓用戶(hù)去選擇照片來(lái)源,拍照或者相冊(cè). UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:0]; UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.delegate = self; imagePickerController.allowsEditing = YES; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@'從相冊(cè)選取' style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;[self presentViewController:imagePickerController animated:YES completion:^{}]; }]; UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@'拍照' style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;[self presentViewController:imagePickerController animated:YES completion:^{}]; }]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@'取消' style:(UIAlertActionStyleCancel) handler:^(UIAlertAction *action) { //這里可以不寫(xiě)代碼 }]; [self presentViewController:alertController animated:YES completion:nil]; //用來(lái)判斷來(lái)源 Xcode中的模擬器是沒(méi)有拍攝功能的,當(dāng)用模擬器的時(shí)候我們不需要把拍照功能加速 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {[alertController addAction:okAction];[alertController addAction:cancelAction];[alertController addAction:photoAction]; } else {[alertController addAction:okAction];[alertController addAction:cancelAction]; }} //這個(gè)是選取完照片后要執(zhí)行的代理方法 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{ [picker dismissViewControllerAnimated:YES completion:^{}]; //選取裁剪后的圖片 UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage]; /* 此處info 有六個(gè)值 * UIImagePickerControllerMediaType; // an NSString UTTypeImage) * UIImagePickerControllerOriginalImage; // a UIImage 原始圖片 * UIImagePickerControllerEditedImage; // a UIImage 裁剪后圖片 * UIImagePickerControllerCropRect; // an NSValue (CGRect) * UIImagePickerControllerMediaURL; // an NSURL * UIImagePickerControllerReferenceURL // an NSURL that references an asset in the AssetsLibrary framework * UIImagePickerControllerMediaMetadata // an NSDictionary containing metadata from a captured photo */ _imageview.image = image;}
相關(guān)文章:
1. Python獲取抖音關(guān)注列表封號(hào)賬號(hào)的實(shí)現(xiàn)代碼2. Warning: require(): open_basedir restriction in effect,目錄配置open_basedir報(bào)錯(cuò)問(wèn)題分析3. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究4. 解決Python 進(jìn)程池Pool中一些坑5. php測(cè)試程序運(yùn)行速度和頁(yè)面執(zhí)行速度的代碼6. Python如何讀寫(xiě)CSV文件7. 三個(gè)不常見(jiàn)的 HTML5 實(shí)用新特性簡(jiǎn)介8. ajax請(qǐng)求添加自定義header參數(shù)代碼9. python利用os模塊編寫(xiě)文件復(fù)制功能——copy()函數(shù)用法10. 無(wú)線標(biāo)記語(yǔ)言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁(yè)
