So I'm developing an app with a friend and I'm trying to wrap my mind around how iOS libraries operate. For whatever reason, my code does not result in the rendering of a user selected photo (from his photo library) to an image view. What am I doing wrong? And, more importantly, how do I debug these sort of problems (I am new to XCode and not sure how debugging works on this platform). Tips and answers are appreciated!
The button function affiliated with this problem is LoadCamRoll and the functions affiliated with what I'm trying to do are imgPicker and imgPickerDidCancel:
import UIKit
import MobileCoreServices
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet var btnBegin: UIButton!
@IBOutlet var txtHome: UITextField!
@IBOutlet var imgLogo: UIImageView!
@IBOutlet var btnCamSwitch: UIButton!
@IBOutlet var btnImageSearch: UIButton!
@IBOutlet var btnBackArrow: UIButton!
@IBOutlet var btnCamRoll: UIButton!
@IBOutlet var btnFlash: UIButton!
@IBOutlet var imgLoaded: UIImageView!
@IBOutlet var imgLineOut: UIImageView!
var isFlashOn = false
var isBackCam = false
let imagePicker = UIImagePickerController()
@IBAction func LoadHome(sender: UIButton) {
btnBegin.removeFromSuperview()
txtHome.removeFromSuperview()
imgLogo.hidden = false
btnFlash.hidden = false
btnCamSwitch.hidden = false
btnImageSearch.hidden = false
btnBackArrow.hidden = false
btnCamRoll.hidden = false
imgLineOut.hidden = false
}
@IBAction func GoBack(sender: UIButton) {
exit(0)
}
@IBAction func LoadCamRoll(sender: UIButton) {
imagePicker.allowsEditing = false
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
presentViewController(imagePicker, animated: true, completion: nil)
}
@IBAction func LoadImgSearch(sender: UIButton) {
let alertImgSearch = UIAlertController(title: "Load Image Search", message: "Image Search Loaded", preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
}
alertImgSearch.addAction(cancelAction)
let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in
}
alertImgSearch.addAction(OKAction)
self.presentViewController(alertImgSearch, animated: true){
}
}
@IBAction func SwitchCam(sender: UIButton) {
let camString = String(isBackCam)
let alertBackCam = UIAlertController(title: "Is Back Camera Active?", message: camString, preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
}
alertBackCam.addAction(cancelAction)
let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in
}
alertBackCam.addAction(OKAction)
self.presentViewController(alertBackCam, animated: true){
}
isBackCam = !isBackCam
}
@IBAction func FlashToggle(sender: UIButton) {
if(isFlashOn == false){
imgLineOut.hidden = false
}
else if(isFlashOn == true){
imgLineOut.hidden = true
}
isFlashOn = !isFlashOn
}
func imgPicker(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject: AnyObject]){
let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
imgLoaded.contentMode = .ScaleAspectFit
imgLoaded.image = selectedImage
dismissViewControllerAnimated(true, completion: nil)
}
func imgPickerDidCancel(picker: UIImagePickerController){
dismissViewControllerAnimated(true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}