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)
}