fb_license

技術標籤

@selector (1) 初使化區塊 (1) 物件 (1) 物件導向 (2) 型別 (4) 封裝 (1) 流程控制 (1) 陣列 (3) 推論型別 (2) 實機測試 (1) 蓋索林(gasolin) (1) 模組 (1) 憑證 (1) 轉型 (1) 羅康鴻 (121) 類別 (1) 變數 (5) Accelerometer (1) ActiveRecord (1) Activity (1) AFNetworking (1) alloc (1) Android (3) Animation (1) App (1) App ID (1) APP上傳 (1) ASP.NET (1) AVAudioPlayer (1) block (1) C# (2) class (1) CLLocationManager (1) CLLocationManagerDelegate (1) CMMotionManager (4) Controller (1) delegate (1) DELETE語法 (1) Device Motion資料 (1) Dialog (1) DropDownList (1) dynamic language (2) Facebook SDK (9) FBRequest (5) FBRequestConnection (2) FMDB (1) Gesture Recognizers (6) GROUP BY (2) Gyro (1) HAVING (1) IBAction (1) IBOutlet (1) id (3) inheritance (1) init (1) Insert (1) instance variable (1) Interface Builder (1) iOS (70) iOS idea (7) iOS Introduction (1) Layout (1) Magnetometer (1) Menu (2) Method (2) MKMapView (1) MKPointAnnotation (1) MS SQL (5) Nil (1) NSArray (1) NSDictionary (1) NSError (1) NSFileManager & .plist (1) NSMutableArray (1) NSMutableDictionary (1) NSNotificationCenter (1) NULL (1) object (2) Objective-C (16) Objective-C idea (1) ORDER BY (1) Parameter (1) property (1) protocol (2) Provisioning (1) Proximate Sensor (1) Q and A (2) R類別 (1) Rails (9) RESTful SOA (9) Ruby (8) Scene (1) SEELECT (1) Segue (2) SEL (1) SELECT語法 (1) Shake (1) Simulator (1) SOA (8) SQL (6) SQL Server (5) SQL函數 (1) SQL彙總函數 SQL (1) SQLite (1) Storyboard (1) Style (1) Swift (1) Table (1) target & action (1) Theme (1) Toast (1) TRUNCATE TABLE語法 (1) UIActionSheet (1) UIActionSheetDelegate (1) UIActivityIndicatorView (1) UIAlertView (1) UIBarButtonItem (1) UIButton (1) UICollectionView (1) UICollectionViewDataSource (1) UIControl (9) UIDatePicker (1) UIImage (1) UIImagePickerController (2) UIImagePickerControllerDelegate (2) UIImageView (1) UILabel (1) UILongPressGestureRecognizer (1) UINavigationController (2) UIPanGestureRecognizer (1) UIPinchGestureRecognizer (1) UIProgressView (1) UIResponder (1) UIRotationGestureRecognizer (1) UISegmentedControl (1) UISlider (1) UIStepper (1) UISwipeGestureRecognizer (1) UISwitch (1) UITabBarController (1) UITableView (1) UITableViewDataSource (1) UITapGestureRecognizer (1) UITextField (1) UITextFieldDelegate (1) UITextView (2) UITextViewDelegate (1) UIToolBar (1) UIView (8) UIWebView (1) UPDATE語法 (1) var (2) VB.NET (7) View (4) WHERE子句 (1) XML (1)

2014/05/15

[iOS] 選取照片 UIImagePickerController & UIImagePickerControllerDelegate


我是UIImagePickerController,我提供關於處理照片的相關的操作,包含選取照片APP中的相片


我是UIImagePickerControllerDelegate,我定義了UIImagePickerController操作照片處理的後續作業,包含選取照片後的後續處理


UIImagePickerController說:「那我們合作吧,在我選取好照片後,在交由實作UIImagePickerControllerDelegate協定的物件,進行照片選取後的處理動作。」





第1步:建立一個名為"UIImagePickerController_UIImagePickerControllerDelegate2"的"Single View Application"專案。


第2步:加入UIImageView,並建立IBOutlet

  • 加入UIImageView
  • 為UIImageView設立IBOutlet - imageView



第3步:加入UIButton,並建立IBAction
  • 加入UIButton,Title設定為"選照片"
  • UIButton並設立IBAction - pickPicture


第4步:顯示相片,透過UIImagePickerControllerDelegate
  • ViewController.h檔中,實作協定UIImagePickerControllerDelegate
  • 在此比較特別的是我們也會加上協定UINavigationControllerDelegate,原因是UIImagePickerController會以使用到Navigation的瀏覽模式,因此需加上,但在此範例我們不需要實作任何UINavigationControllerDelegate的方法
ViewController.h
#import <UIKit/UIKit.h> @interface ViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate> @property (strong, nonatomic) IBOutlet UIImageView *imageView; - (IBAction)pickPicture:(id)sender; @end
  • 於ViewController.m檔中,實作imagePickerController:didFinishPickingMediaWithInfo:方法,以將取得的相片儲存
ViewController.m

//選取相片後的處理, //在此用以顯示選取的圖片於imageView中 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { //取得選取的相片 UIImage * image = [info valueForKey:UIImagePickerControllerOriginalImage]; //設定予imageView顯示 self.imageView.image = image; //關閉目前顯示的Controller,即UIImagePickerController [self dismissViewControllerAnimated:YES completion:nil]; }

第5步:選取相片,透過UIImagePickerController
  • 於ViewController.m檔中,透過UIImagePickerController進行拍照
  • 同時委派給self(即ViewController),以進行拍照後的處理,而ViewController因實作了UIImagePickerControllerDelegate協定,會透過imagePickerController:didFinishPickingMediaWithInfo:方法將相片儲存在照片(即iOS的照片APP)中

ViewController.m
- (IBAction)pickPicture:(id)sender { //建立UIImagePickerController UIImagePickerController * controller = [[UIImagePickerController alloc] init]; //設定選取照片的來源為PhotoLibrary,即照片APP中的相片 controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; ////委派給self,即目前的controller, //以處後取得相片後的顯示相片的作業 //當然目前的controller已實作UINavigationControllerDelegate //與 UIImagePickerControllerDelegate 2個協定 controller.delegate = self; //轉換至UIImagePickerController,以選取照片 [self presentViewController:controller animated:YES completion:nil]; }

第5步:執行
  • 點"選照片"後,即可在挑選特定照片後,在原先的畫面上方顯示挑選的照片





檔案連結:UIImagePickerController_UIImagePickerControllerDelegate2.zip



更多UIImagePickerController的資訊: