1.实现这些Delegate,UIActionSheetDelegate,UIImagePickerControllerDelegate, UINavigationControllerDelegate 2.为了方便测试添加两个控件 var btnChoose = UIButton(frame: CGRect(x: 20, y: 40, width: 100, height: 50)) btnChoose.setTitle("设置照片", forState: UIControlState.Normal) btnChoose.backgroundColor = UIColor.grayColor() btnChoose.addTarget(self, action: "chooseImgAction", forControlEvents: UIControlEvents.TouchUpInside) self.view.addSubview(btnChoose) imgShow = UIImageView(frame: CGRect(x: 20, y: 120, width: 100, height: 100)) imgShow.backgroundColor = UIColor.grayColor() self.view.addSubview(imgShow) 3.实现各种点击事件和弹出框 func chooseImgAction() { var sheet:UIActionSheet if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)) { sheet = UIActionSheet(title: "选择头像", delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: nil, otherButtonTitles: "从相册选择","拍照") }else { sheet = UIActionSheet(title:"选择头像", delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: nil, otherButtonTitles: "从相册选择") } sheet.showInView(self.view) } func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) { var sourceType = UIImagePickerControllerSourceType.PhotoLibrary if(buttonIndex != 0) { if(buttonIndex==1) { //相册 sourceType = UIImagePickerControllerSourceType.PhotoLibrary }else{ sourceType = UIImagePickerControllerSourceType.Camera } let imagePickerController:UIImagePickerController = UIImagePickerController() imagePickerController.delegate = self imagePickerController.allowsEditing = true//true为拍照、选择完进入图片编辑模式 imagePickerController.sourceType = sourceType self.presentViewController(imagePickerController, animated: true, completion: { }) } } func imagePickerControllerDidCancel(picker:UIImagePickerController) { self.dismissViewControllerAnimated(true, completion: nil) } func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) { imgShow.image = image self.dismissViewControllerAnimated(true, completion: nil) }