国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > iOS > 正文

IOS打開(kāi)照相機(jī)與本地相冊(cè)選擇圖片實(shí)例詳解

2019-10-21 18:45:55
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

IOS打開(kāi)照相機(jī)與本地相冊(cè)選擇圖片

最近正好項(xiàng)目里面要集成“打開(kāi)照相機(jī)與本地相冊(cè)選擇圖片”的功能,今天就在這邊給大家寫一個(gè)演示程序;打開(kāi)相機(jī)拍攝后或者在相冊(cè)中選擇一張照片,然后將它顯示在界面上。好了廢話不多說(shuō),因?yàn)楸容^簡(jiǎn)單直接上源碼。

首先,我們?cè)陬^文件中添加需要用到的actionSheet控件,顯示圖片的UIImageView控件,并且加上所需要的協(xié)議

#import <UIKit/UIKit.h>  @interface ImagePickerViewController : UIViewController<UIImagePickerControllerDelegate,UIActionSheetDelegate,UINavigationControllerDelegate>  @property (strong, nonatomic) IBOutlet UIImageView *headImage;  @property (strong, nonatomic) UIActionSheet *actionSheet;  - (IBAction)clickPickImage:(id)sender; @end 

通過(guò)點(diǎn)擊我設(shè)置在界面中的按鈕來(lái)呼出actionSheet控件,來(lái)選擇相應(yīng)的操作拍照或是在相冊(cè)中選擇相片,代碼如下:

// // ImagePickerViewController.m // testAuto // // Created by silicon on 15/5/9. // Copyright (c) 2015年 silicon. All rights reserved. //  #import "ImagePickerViewController.h"  @interface ImagePickerViewController ()  @end  @implementation ImagePickerViewController  @synthesize actionSheet = _actionSheet;  - (void)viewDidLoad {   [super viewDidLoad];   // Do any additional setup after loading the view from its nib.    }  - (void)didReceiveMemoryWarning {   [super didReceiveMemoryWarning];   // Dispose of any resources that can be recreated. }   /**  @ 調(diào)用ActionSheet  */ - (void)callActionSheetFunc{   if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){     self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"選擇圖像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"從相冊(cè)選擇", nil nil];   }else{     self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"選擇圖像" delegate:self cancelButtonTitle:@"取消"destructiveButtonTitle:nil otherButtonTitles:@"從相冊(cè)選擇", nil nil];   }      self.actionSheet.tag = 1000;   [self.actionSheet showInView:self.view]; }  // Called when a button is clicked. The view will be automatically dismissed after this call returns - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{   if (actionSheet.tag == 1000) {     NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;     // 判斷是否支持相機(jī)     if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {       switch (buttonIndex) {         case 0:           //來(lái)源:相機(jī)           sourceType = UIImagePickerControllerSourceTypeCamera;           break;         case 1:           //來(lái)源:相冊(cè)           sourceType = UIImagePickerControllerSourceTypePhotoLibrary;           break;         case 2:           return;       }     }     else {       if (buttonIndex == 2) {         return;       } else {         sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;       }     }     // 跳轉(zhuǎn)到相機(jī)或相冊(cè)頁(yè)面     UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];     imagePickerController.delegate = self;     imagePickerController.allowsEditing = YES;     imagePickerController.sourceType = sourceType;          [self presentViewController:imagePickerController animated:YES completion:^{          }];   } }  - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {   [picker dismissViewControllerAnimated:YES completion:^{      }];      UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];   self.headImage.image = image; }  /* #pragma mark - Navigation  // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {   // Get the new view controller using [segue destinationViewController].   // Pass the selected object to the new view controller. } */  - (IBAction)clickPickImage:(id)sender {      [self callActionSheetFunc]; } @end 

代碼比較簡(jiǎn)單,也容易理解,運(yùn)行的效果如下:

IOS,照相機(jī),本地相冊(cè),選擇圖片,IOS照相機(jī)

IOS,照相機(jī),本地相冊(cè),選擇圖片,IOS照相機(jī)

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到IOS開(kāi)發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 罗城| 揭阳市| 乐清市| 威海市| 南部县| 泽库县| 巴塘县| 清镇市| 满城县| 四子王旗| 华坪县| 新密市| 和静县| 杭锦后旗| 南陵县| 三原县| 册亨县| 衡水市| 泌阳县| 陆川县| 敦化市| 四平市| 罗甸县| 绥滨县| 册亨县| 迁安市| 韶山市| 五华县| 青铜峡市| 新田县| 武邑县| 高清| 精河县| 章丘市| 林甸县| 洛阳市| 进贤县| 禹城市| 蒙自县| 宁远县| 富裕县|