色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

ios 照相機和相冊的調用

瀏覽:20日期:2022-09-17 13:38:13

一個簡單的功能,上傳照片或者拍照可以用到.

//首先遵循兩個代理 <UIImagePickerControllerDelegate,UINavigationControllerDelegate> //我們創建一個btn和一個imageview,btn用來觸發事件調起照相機和相冊的功能,imageview用來展示選取或者拍攝的圖片. 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的點擊事件 - (void)btnClick:(UIButton *)btn{ //創建UIAlertController是為了讓用戶去選擇照片來源,拍照或者相冊. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:0]; UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.delegate = self; imagePickerController.allowsEditing = YES; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@'從相冊選取' 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) { //這里可以不寫代碼 }]; [self presentViewController:alertController animated:YES completion:nil]; //用來判斷來源 Xcode中的模擬器是沒有拍攝功能的,當用模擬器的時候我們不需要把拍照功能加速 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {[alertController addAction:okAction];[alertController addAction:cancelAction];[alertController addAction:photoAction]; } else {[alertController addAction:okAction];[alertController addAction:cancelAction]; }} //這個是選取完照片后要執行的代理方法 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{ [picker dismissViewControllerAnimated:YES completion:^{}]; //選取裁剪后的圖片 UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage]; /* 此處info 有六個值 * 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;}

標簽: IOS
上一條:淺談iOS的文件操作下一條:iOS繪圖
相關文章:
主站蜘蛛池模板: 久草在线视频在线观看 | 欧美毛片一级的免费的 | 国产精品99久久久久久www | 伊大人香蕉久久网欧美 | 日本特黄a级高清免费酷网 日本特黄特色 | 日韩一级视频在线观看播放 | 亚洲高清在线观看播放 | 91国偷自产一区二区三区 | 18videosex性欧美69超高清 | 欧美黑寡妇特a级做爰 | 亚洲jjzzjjzz在线播放 | 亚洲天堂一区二区在线观看 | 亚洲看片网| 国产自产在线 | 久久国产成人福利播放 | 亚洲深夜福利视频 | 成人免费视频网址 | 国产成人一区二区三区精品久久 | 中文字幕在线一区二区在线 | 成人一a毛片免费视频 | 国产视频久久 | 久草视频大全 | 亚洲系列中文字幕一区二区 | 亚洲素人在线 | 美女张开腿让男人桶爽免费网站 | 国产成人综合91精品 | 久久精品国产亚洲网址 | 国产成人精品一区二区三区 | 初爱视频教程在线观看高清 | 日本b站一卡二不卡 | 亚洲男人天堂久久 | 欧美毛片aaa激情 | 成人网18免费网站在线 | 欧美一级精品 | 国产精品无圣光一区二区 | 国产精品二区页在线播放 | 国产亚洲一区二区三区不卡 | 亚洲精品中文字幕一区在线 | 久久久www免费人成看片 | 日韩视频在线观看 | 97视频免费公开成人福利 |