iOS開發-照片選擇

來源:互聯網
上載者:User

標籤:

本來想做個註冊登入的表單的,想想還是先做個簡單的頭像選擇,一般情況下不管是內部管理系統還是面向公眾的互連網公司,註冊登入是免不了的,帳戶圖片上傳是免不了的,尤其是企業使用者,上傳了自己的圖片才感覺自己買的系統是比較值的。iOS開發中上傳圖片還是比較簡單的,通過UIImagePickerController和協議就很容易的實現的圖片的選擇和上傳。

頁面配置

首先看下視圖,一個按鈕,一個標籤和一個ImageView:

介面很簡單,標頭檔中聲明一下UIImageView:

@property (weak, nonatomic) IBOutlet UIImageView *imageView;
 Demo實現

上傳按鈕的代碼,具體方法已經注釋:

 

//選擇頭像- (IBAction)chooseImage:(id)sender {    //執行個體化照片選擇控制器    UIImagePickerController *pickControl=[[UIImagePickerController alloc]init];        //設定照片源    [pickControl setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];        //設定協議    [pickControl setDelegate:self];        //設定編輯    [pickControl setAllowsEditing:YES];        //選完圖片之後回到的視圖介面    [self presentViewController:pickControl animated:YES completion:nil];    }

 

設定協議,需要標頭檔中聲明一下:

@interface ViewController : UIViewController <UINavigationControllerDelegate,UIImagePickerControllerDelegate>@property (weak, nonatomic) IBOutlet UIImageView *imageView;@end

之後需要在返回的時候顯示圖片使用協議中的imagePickerController方法:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{    //    UIImage *image=info[@"UIImagePickerControllerOriginalImage"];         UIImage *image=info[@"UIImagePickerControllerEditedImage"];        [self.imageView setImage:image];        //選取完圖片之後關閉視圖    [self dismissViewControllerAnimated:YES completion:nil];   }

 需要注意的,如果需要不需要編輯,擷取image的方法為:

UIImage *image=info[@"UIImagePickerControllerOriginalImage"];

 最終效果示範:

 

iOS開發-照片選擇

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.