xcode - SWIFT : Accessing picture library in an ActionSheet style -


i'm trying create profilepic in sign view controller. profilepic of type uiimageview. want able tap instantly pulls photo library in actionsheet style. , have 1 of image box button access camera. possible?

import uikit  class signupviewcontroller: uiviewcontroller, uitextfielddelegate, uipickerviewdatasource, uipickerviewdelegate {      @iboutlet weak var profilepic: uiimageview!       override func viewdidload() {         super.viewdidload()          // additional setup after loading view.          // profile picture          let tapgesture = uitapgesturerecognizer(target: self, action: "imagetapped:")          profilepic.addgesturerecognizer(tapgesture)         profilepic.userinteractionenabled = true      }       func imagetapped(gesture:uigesturerecognizer) {         if let profile1pic = gesture.view as? uiimageview {             print("image tapped")         }     } 

try code below :

import uikit  class signupviewcontroller: uiviewcontroller, uiimagepickercontrollerdelegate {  @iboutlet weak var profilepic: uiimageview!   override func viewdidload() {   super.viewdidload()    let tapgesture = uitapgesturerecognizer(target: self, action: "imagetapped:")    profilepic.addgesturerecognizer(tapgesture)   profilepic.userinteractionenabled = true  }   func imagetapped(gesture:uigesturerecognizer) {   if let profile1pic = gesture.view as? uiimageview {       print("image tapped")       showactionsheet()   } } func camera() {     var mypickercontroller = uiimagepickercontroller()     mypickercontroller.delegate = self;     mypickercontroller.sourcetype = uiimagepickercontrollersourcetype.camera      self.presentviewcontroller(mypickercontroller, animated: true, completion: nil)  }  func photolibrary() {      var mypickercontroller = uiimagepickercontroller()     mypickercontroller.delegate = self;     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]) {      profilepic.image = info[uiimagepickercontrolleroriginalimage] as? uiimage     self.dismissviewcontrolleranimated(true, completion: nil)  } } 

its working on side..hope !


Comments