IOS打開照相機(jī)與本地相冊(cè)選擇圖片實(shí)例詳解
IOS打開照相機(jī)與本地相冊(cè)選擇圖片
最近正好項(xiàng)目里面要集成“打開照相機(jī)與本地相冊(cè)選擇圖片”的功能,今天就在這邊給大家寫一個(gè)演示程序;打開相機(jī)拍攝后或者在相冊(cè)中選擇一張照片,然后將它顯示在界面上。好了廢話不多說,因?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
通過點(diǎn)擊我設(shè)置在界面中的按鈕來呼出actionSheet控件,來選擇相應(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:
//來源:相機(jī)
sourceType = UIImagePickerControllerSourceTypeCamera;
break;
case 1:
//來源:相冊(cè)
sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
break;
case 2:
return;
}
}
else {
if (buttonIndex == 2) {
return;
} else {
sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
}
// 跳轉(zhuǎn)到相機(jī)或相冊(cè)頁面
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)行的效果如下:


感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
IOS初始化控制器的實(shí)現(xiàn)方法總結(jié)
這篇文章主要介紹了IOS初始化控制器的實(shí)現(xiàn)方法總結(jié)的相關(guān)資料,這里提供兩種實(shí)現(xiàn)方法分別是ViewControllViewController方法和 ViewControllViewController 與 xib方法,需要的朋友可以參考下2017-10-10
詳解iOS 驗(yàn)證碼輸入的實(shí)現(xiàn)思路
這篇文章主要介紹了iOS 驗(yàn)證碼輸入一種實(shí)現(xiàn)思路,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-10-10
iOS使用 CABasicAnimation 實(shí)現(xiàn)簡(jiǎn)單的跑馬燈(無cpu暴漲)
本篇文章主要介紹了iOS使用 CABasicAnimation 實(shí)現(xiàn)簡(jiǎn)單的跑馬燈(無cpu暴漲),具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01
Objective-C中block循環(huán)引用問題詳解
這篇文章主要給大家介紹了關(guān)于Objective-C中block循環(huán)引用問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Objective-C具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
IOS開發(fā)中如何設(shè)計(jì)短信驗(yàn)證碼防刷機(jī)制
給大家詳細(xì)分享一下在IOS的項(xiàng)目開發(fā)中如何設(shè)計(jì)短信驗(yàn)證碼防刷機(jī)制,已經(jīng)步驟詳解,喜歡的朋友參考下吧。2018-02-02
CAMediaTiming ( 時(shí)間協(xié)議)詳解及實(shí)例代碼
這篇文章主要介紹了CAMediaTiming / 時(shí)間協(xié)議詳解及實(shí)例代碼的相關(guān)資料,這里附有實(shí)例代碼,幫助大家學(xué)習(xí)參考,需要的朋友可以參考下2016-12-12
iOS開源一個(gè)簡(jiǎn)單的訂餐app UI框架
這篇文章主要介紹了iOS開源一個(gè)簡(jiǎn)單的訂餐app UI框架,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
iOS應(yīng)用開發(fā)中監(jiān)聽鍵盤事件的代碼實(shí)例小結(jié)
這篇文章主要介紹了iOS應(yīng)用開發(fā)中監(jiān)聽鍵盤事件的代碼實(shí)例小結(jié),呼出鍵盤等操作為iOS App中的必備功能,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03
iOS和JS交互教程之WKWebView-協(xié)議攔截詳解
這篇文章主要給大家介紹了關(guān)于iOS和JS交互教程之WKWebView-協(xié)議攔截的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09

