i'm new to the whole iOS development. I would like to ask, I have a vc with an image view and a button ( text fields & labels are not the problem) and i want when the user taps to choose or to take a profile photo. The photo that the user will take or choose, it's going to replace my static image view. How i can achieve this? Here is the vc
and here is my code:
import UIKit
class CreateAccountViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextFieldDelegate {
@IBOutlet weak var profilePic: UIImageView!
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var emailTxtField: UITextField!
@IBOutlet weak var pwdTextFld: UITextField!
@IBOutlet weak var retypePwdTextField: UITextField!
@IBOutlet weak var startEarningRewardsBtn: UIButton!
let myPickerController = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
startEarningRewardsBtn.layer.cornerRadius = 4
UIApplication.sharedApplication().statusBarStyle = .Default
myPickerController.delegate = self
let tapGesture = UITapGestureRecognizer(target: self, action:
"imageTapped:")
profilePic.addGestureRecognizer(tapGesture)
profilePic.userInteractionEnabled = true
}
func imageTapped(gesture:UIGestureRecognizer) {
if let _ = gesture.view as? UIImageView {
print("Image Tapped")
showActionSheet()
}
}
func camera() {
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = UIImagePickerControllerSourceType.Camera
self.presentViewController(myPickerController, animated: true, completion: nil)
}
func photoLibrary() {
let myPickerController = UIImagePickerController()
myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(myPickerController, animated: true, completion: nil)
}
func showActionSheet() {
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
actionSheet.addAction(UIAlertAction(title: "Camera", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -> Void in
self.camera()
}))
actionSheet.addAction(UIAlertAction(title: "Gallery", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -> Void in
self.photoLibrary()
}))
actionSheet.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
self.presentViewController(actionSheet, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject!]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
profilePic.contentMode = .ScaleAspectFit
profilePic.image = pickedImage
}
self.dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
}