Xcode 8 - swift 3: Creating an image format with an unknown type is an error - swift3

I get an error when selecting an image with UIImagePicker:
[Generic] Creating an image format with an unknown type is an error
I'm using Xcode 8.1 and Swift 3.
I've already searched all around the web but nothing seems to solve my problem, please help!
Here it's my code:
class TabBarController: UITabBarController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var isSelected: Bool = false
var imgPicker: UIImagePickerController = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
//imgPicker = UIImagePickerController()
imgPicker.delegate = self
imgPicker.allowsEditing = false
imgPicker.sourceType = .photoLibrary
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
imgPicker.dismiss(animated: true, completion: nil)
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
self.performSegue(withIdentifier: "toPostPicture", sender: image)
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
func askImagePickerSource(sender: UIViewController) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let cameraAction = UIAlertAction(title: "Camera", style: .default, handler: { (action: UIAlertAction) in
self.imgPicker.sourceType = .camera
self.imgPicker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .camera)!
sender.present(self.imgPicker, animated: true, completion: nil)
})
let photoLibraryAction = UIAlertAction(title: "Photo Library", style: .default, handler: { (action: UIAlertAction) in
self.imgPicker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
sender.present(self.imgPicker, animated: true, completion: nil)
})
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (UIAlertAction) in
sender.dismiss(animated: true, completion: nil)
}
alert.addAction(cameraAction)
alert.addAction(photoLibraryAction)
alert.addAction(cancelAction)
sender.present(alert, animated: true, completion: nil)
} else {
self.imgPicker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
sender.present(self.imgPicker, animated: true, completion: nil)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toPostPicture" {
if let postPictureVC = segue.destination as? PostPictureVC {
if let image = sender as? UIImage {
postPictureVC.image = image
}
}
}
}
}

I faced the same issue by not adding delegate as 'self' for the UIImagePickerController. After adding it I was able to get the selected image from gallery. But even after adding delegate as self and able to access the image (Display on an ImageView), I was still getting the same error. After a further research on SO I got to know that this is just a bug and doesn't affect the app in any way.
See also "Creating an image format with an unknown type is an error Objective-C Xcode 8"

Can you post the .debugDescription of the info parameter in your picker delegate?
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : Any]) {
print(info.debugDescription)
...
}

Related

After dismiss the imagePickerController, how to go back to the current tab instead of the default tab

I have 4 tabs in the tab bar. Currently, I am working in the 4th tab, and having an imagePickerController. After the imagePickerController dismissed, it goes to the default tab (1st tab). How can I make it go to the current (4th) tab?
class EditMyPostViewController: UIViewController {
#IBOutlet weak var postImageView: UIImageView!
let imagePicker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
}
#IBAction func updatePhotoTapped(_ sender: Any) {
let alert = UIAlertController(title: "Add a picture", message: "How do you want to upload the picture?", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Take Photo", style: .default, handler: { action in
self.takePhotoWithCamera()
}))
alert.addAction(UIAlertAction(title: "Use Existing Photo", style: .default, handler: { action in
self.getPhotoFromLibrary()
}))
self.present(alert, animated: true)
}
}
extension EditMyPostViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func takePhotoWithCamera() {
if (!UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera)) {
let alertController = UIAlertController(title: "No Camera", message: "The device has no camera.", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(okAction)
present(alertController, animated: true, completion: nil)
} else {
imagePicker.allowsEditing = false
imagePicker.sourceType = .camera
present(imagePicker, animated: true, completion: nil)
}
}
func getPhotoFromLibrary() {
imagePicker.allowsEditing = false
imagePicker.sourceType = .photoLibrary
present(imagePicker, animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
{
postImageView.image = image
}
dismiss(animated: true, completion: nil)
}
}
The imagePicker is present in the 2 functions takePhotoWithCamera() and getPhotoFromLibrary().
In the last function, I tried to dismiss(animated: true, completion: nil), but it goes to the default tab instead of the current tab. How can I present the current ViewController?
I tried
present(self, animated: true, completion: nil)
But it will cause an Exception: Application tried to present modally an active controller

Creating an image format with an unknown type is an error UIImagePickerController() , unexpectedly found nil

Swift Version 3
Xcode Version 8.3.3 (8E3004b)
This question has been asked and answered before. Yet, I cannot find a way fix this error.
"Creating an image format with an unknown type is an error" with UIImagePickerController
When a new picture is selected from Photo Library, I get this error
Generic Creating an image format with an unknown type is an error.
I select the photo and when I tap choose, I get error in func takePhoto where I do this assignment self?.imgView.image = newImage
fatal error: unexpectedly found nil while unwrapping an Optional value
Printing description of newImage: expression produced error: error:
/var/folders/s0/xq_zc7l56m1__n1qjk29p6tw0000gn/T/./lldb/2631/expr1.swift:1:80:
error: use of undeclared type 'UIKit'
Swift._DebuggerSupport.stringForPrintObject(Swift.UnsafePointer>(bitPattern:
0x118055200)!.pointee)
^~~~~
typealias imagePickerHelperCompletion = ((UIImage?) -> Void)!
class ImagePickerHelper: NSObject {
weak var viewController: UIViewController!
var cameraFlashMode: UIImagePickerControllerCameraFlashMode //flash, no flash
var photoLibraryIsVisible: Bool
var cameraDevice: UIImagePickerControllerCameraDevice //front, back camera
var completion: imagePickerHelperCompletion
init(viewController: UIViewController, cameraFlashMode:UIImagePickerControllerCameraFlashMode, photoLibraryIsVisible: Bool,cameraDevice: UIImagePickerControllerCameraDevice, completion: imagePickerHelperCompletion) {
self.viewController = viewController
self.completion = completion
self.cameraFlashMode = cameraFlashMode
self.photoLibraryIsVisible = photoLibraryIsVisible
self.cameraDevice = cameraDevice
//super.init() implemented by subclasses to initializer a new object (the receiver) immediately after memory has been allocated
super.init()
self.showPhotoSourceSelection()
}
//show camera or photo library
func showPhotoSourceSelection() {
let actionSheet = UIAlertController(title: "Take new photo", message: "Would you like to open \n Camera or Photo Library?", preferredStyle: .actionSheet)
let cameraAction = UIAlertAction(title: "Camera", style: .default) { (action) in
self.showImagePicker(sourceType: .camera)
}
let photoLibraryAction = UIAlertAction(title: "Photo Library", style: .default) { (action) in
self.showImagePicker(sourceType: .photoLibrary)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in }
actionSheet.addAction(cameraAction)
if photoLibraryIsVisible == true { actionSheet.addAction(photoLibraryAction) }
actionSheet.addAction(cancelAction)
viewController.present(actionSheet, animated: true, completion: nil)
}
func showImagePicker(sourceType: UIImagePickerControllerSourceType) {
let imagePicker = UIImagePickerController()
imagePicker.allowsEditing = true //user can crop the image, or do some editing
imagePicker.sourceType = sourceType
imagePicker.mediaTypes = [kUTTypeImage as String]
imagePicker.delegate = self
if imagePicker.sourceType == .camera {
imagePicker.cameraFlashMode = cameraFlashMode
imagePicker.cameraDevice = self.cameraDevice
}
viewController.present(imagePicker, animated: true, completion: nil)
}
}//end of class
extension ImagePickerHelper: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
viewController.dismiss(animated: true, completion: nil)
}
//if user did take a photo
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
print(info)
//prints out ["UIImagePickerControllerEditedImage": <UIImage: 0x610000294320> size {746, 498} orientation 0 scale 1.000000, "UIImagePickerControllerMediaType": public.image, "UIImagePickerControllerCropRect": NSRect: {{0, 0}, {2661, 1779}}, "UIImagePickerControllerReferenceURL": assets-library://asset/asset.JPG?id=ED7AC36B-A150-4C38-BB8C-B6D696F4F2ED&ext=JPG, "UIImagePickerControllerOriginalImage": <UIImage: 0x610000299320> size {2668, 1780} orientation 0 scale 1.000000]
if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
completion(image)
viewController.dismiss(animated: true, completion: nil)
} else if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
viewController.dismiss(animated: true, completion: nil)
completion(image)
} else {
print("something is wrong line \(#line)")
}
}
}//end of extension
class ProfileViewController: UITableViewController {
var imagePickerHelper: ImagePickerHelper!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func takePhoto(_ sender: Any) {
//create instance of ImagePickerHelper and take photo, or get it from photoLibrary
imagePickerHelper = ImagePickerHelper( viewController: self, cameraFlashMode: .auto,photoLibraryIsVisible: true, cameraDevice: .front,
completion: { [weak self] (newImage) in
})
}
}//end of class

Presenting image picker using TapGesture on UIImageView

I have a View with an UIImageView that I want to be 'selectable' so that the user can pick a new image.
The function for picking the new image is in the Controller.
Question
How do I call the myDatasourceController.handleTap() function by pressing the ImageView, so that the image picker is presented?
This is an example of my current setup
View
class myView: UICollectionViewCell {
lazy var profileImageView: UIImageView = {
let iv = UIImageView()
iv.isUserInteractionEnabled = true
iv.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(myDatasourceController.handleTap)))
return iv
}()
}
Controller
class myDatasourceController: UICollectionViewController,
UICollectionViewDelegateFlowLayout, UIImagePickerControllerDelegate,
UINavigationControllerDelegate {
func handleTap(){
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.allowsEditing = true
present(imagePickerController, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
// logic for picking the image
dismiss(animated: true, completion: nil)
}
}
This setup currently throws the error
unrecognized selector sent to instance 0x7f9163d493f0
which has led me to try various combinations of
handleTap(_:)
handleTap(sender: UITapGestureRecogniser)
/// etc
but I can't get any of them to work. How should I be constructing my View, Controller, and the interaction between them to present the image picker?
Use Like this
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(RegisterViewController. handleTap(gesture:)))
func handleTap(gesture: UIGestureRecognizer) {
// if the tapped view is a UIImageView then set it to imageview
if (gesture.view as? UIImageView) != nil {
print("Image Tapped")
picker.allowsEditing = false
picker.sourceType = .photoLibrary
picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
present(picker, animated: true, completion: nil)
}
}
Use like this :
myDatasourceController.handleTap()
In your code :
iv.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(myDatasourceController.handleTap())))
The key to the solution is to implement a protocol / delegate, as suggested by #Akis
I've uploaded the full project to my github account. The key code is copied here.
View Controller
protocol ImagePickerDelegate: class {
func loadImagePicker()
}
class HomeViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout, UIImagePickerControllerDelegate, UINavigationControllerDelegate, ImagePickerDelegate {
let cellId = "cellId"
func loadImagePicker(){
print(" -- image picker -- ")
// load image picker
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.allowsEditing = true
present(imagePickerController, animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
// get the image
var selectedImageFromPicker: UIImage?
if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage {
selectedImageFromPicker = editedImage
}else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage {
selectedImageFromPicker = originalImage
}
if let selectedImage = selectedImageFromPicker {
//doSomethingWithTheImage(image: selectedImage)
}
dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.backgroundColor = .black
collectionView?.register(HomeView.self, forCellWithReuseIdentifier: cellId)
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! HomeView
cell.delegate = self
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: view.frame.height)
}
}
View
class HomeView: UICollectionViewCell {
// break retain cycle with weak var
weak var delegate: ImagePickerDelegate?
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
lazy var profileImageView: UIImageView = {
let iv = UIImageView()
iv.isUserInteractionEnabled = true
iv.image = UIImage(named: "kuang-si-falls-waterfall-water-laos-50588.jpg")
iv.contentMode = .scaleAspectFill
let tap = UITapGestureRecognizer(target: self, action: #selector(loadImagePicker))
iv.addGestureRecognizer(tap)
return iv
}()
func loadImagePicker() {
delegate?.loadImagePicker()
print(" imagePickerProtocol called ")
}
func setupViews() {
backgroundColor = .white
addSubview(profileImageView)
profileImageView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
profileImageView.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
profileImageView.frame = CGRect(x: 0, y: 0, width: 150, height: 150)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

UILabel data to UIAlert issue in xcode 8, error

I made an alert and I tried to follow a tutorial and put it into my code. I put the code in a few different places but the errors just kept getting worse. I finally just put it at the bottom, because thats where I ended up getting the fewest errors. I'm new to xcode so this very basic thing is very hard for me. Furthermore, I get errors all over the place when I put this in, and I have no idea how to fix this. Moreover, what I am trying to do is take the data that is being saved in my UILabel which is a name, and I want that to be shown up in the clickable part of the alert that "dismisses" the alert, but I have no idea how to do that or even get started when I can't add a basic alert into my code. Any help would be great source code even better. Sorry for all the questions. Thanks again in advance.
import UIKit
import MultipeerConnectivity
class ViewController: UIViewController, MCBrowserViewControllerDelegate {
#IBOutlet weak var input: UITextField!
#IBOutlet weak var output: UILabel!
#IBAction func action(_ sender: Any) {
output.text = input.text
UserDefaults.standard.set(input.text, forKey: "MyName")
input.text = ""
}
var currentPlayer:String!
var appDelegate:AppDelegate!
override func viewDidLoad() {
super.viewDidLoad()
appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.MPCHandler.setupPeerWithDisplayName(UIDevice.current.name)
appDelegate.MPCHandler.setupSession()
appDelegate.MPCHandler.advertiseSelf(true)
NotificationCenter.default.addObserver(self, selector: Selector(("peerChangedStateWithNotification:")), name: NSNotification.Name(rawValue: "MPC_DidChangeStateNotification"), object: nil)
NotificationCenter.default.addObserver(self, selector: Selector(("handleReceivedDataWithNotification:")), name: NSNotification.Name(rawValue: "MPC_DidReceiveDataNotification"), object: nil)
}
#IBAction func connect(_ sender: Any) {
if appDelegate.MPCHandler.session != nil{
appDelegate.MPCHandler.setupBrowser()
appDelegate.MPCHandler.browser.delegate = self
self.present(appDelegate.MPCHandler.browser, animated: true, completion: nil)
}
}
func peerChangedStateWithNotification(notification:NSNotification){
let userInfo = NSDictionary(dictionary: notification.userInfo!)
let state = userInfo.object(forKey: "state") as! Int
if state != MCSessionState.connecting.rawValue{
self.navigationItem.title = "Connected"
}
}
func browserViewControllerDidFinish(_ browserViewController: MCBrowserViewController) {
appDelegate.MPCHandler.browser.dismiss(animated: true, completion: nil)
}
func browserViewControllerWasCancelled(_ browserViewController: MCBrowserViewController) {
appDelegate.MPCHandler.browser.dismiss(animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidAppear(_ animated: Bool) {
if let x = UserDefaults.standard.object(forKey:"myName") as?
String
{
output.text = x
}
}
}
func viewDidAppear(_animated: Bool) {
createAlert(title: "HI", message: "ARE YOU READY")
}
func createAlert (title: String, message:String)
{
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "HI", style: UIAlertActionStyle.default, handler: { (action) in alert.dismiss(animated: true, completion: nil);))
self.present(alert,animated: true, completion:nil)
}
}
The error you are most probably getting is Invalid redeclaration of 'viewDidAppear' means you are trying to add viewDidAppear method twice in your ViewController. So remove the below one method form your code and call createAlert inside the already exist viewDidAppear one.
Your second mistake is you forgot to add } for UIAlertActionHandler and there is no need to call dismiss on alert action it will automatically dismiss the alert.
You need to also change your selector syntax to Swift3 one and also you forgot to add handleReceivedDataWithNotification with your code.
Now with Swift use Swift native dictionary type instead of NSDictionary, so change your controller with below one to get your desired output.
class ViewController: UIViewController, MCBrowserViewControllerDelegate {
#IBOutlet weak var input: UITextField!
#IBOutlet weak var output: UILabel!
var currentPlayer:String!
var appDelegate:AppDelegate!
override func viewDidLoad() {
super.viewDidLoad()
appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.MPCHandler.setupPeerWithDisplayName(UIDevice.current.name)
appDelegate.MPCHandler.setupSession()
appDelegate.MPCHandler.advertiseSelf(true)
NotificationCenter.default.addObserver(self, selector: #selector(peerChangedStateWithNotification(_:)), name: NSNotification.Name(rawValue: "MPC_DidChangeStateNotification"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleReceivedDataWithNotification(_:)), name: NSNotification.Name(rawValue: "MPC_DidReceiveDataNotification"), object: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func connect(_ sender: Any) {
if appDelegate.MPCHandler.session != nil{
appDelegate.MPCHandler.setupBrowser()
appDelegate.MPCHandler.browser.delegate = self
self.present(appDelegate.MPCHandler.browser, animated: true, completion: nil)
}
}
#IBAction func action(_ sender: Any) {
output.text = input.text
UserDefaults.standard.set(input.text, forKey: "MyName")
input.text = ""
}
func peerChangedStateWithNotification(_ notification: Notification) {
let userInfo = notification.userInfo!
let state = userInfo["state"] as! Int
if state != MCSessionState.connecting.rawValue{
self.navigationItem.title = "Connected"
}
}
func handleReceivedDataWithNotification(_ notification: Notification) {
let userInfo = notification.userInfo!
print(userInfo)
}
func browserViewControllerDidFinish(_ browserViewController: MCBrowserViewController) {
appDelegate.MPCHandler.browser.dismiss(animated: true, completion: nil)
}
func browserViewControllerWasCancelled(_ browserViewController: MCBrowserViewController) {
appDelegate.MPCHandler.browser.dismiss(animated: true, completion: nil)
}
override func viewDidAppear(_ animated: Bool) {
if let x = UserDefaults.standard.string(forKey: "myName") {
output.text = x
}
else {
output.text = "Default Name" //Set here default Name
}
self.createAlert(title: "HI", message: "ARE YOU READY")
}
func createAlert (title: String, message:String)
{
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: output.text, style: UIAlertActionStyle.default, handler: { (action) in
}))
self.present(alert,animated: true, completion:nil)
}
}
import UIKit
import MultipeerConnectivity
class ViewController: UIViewController, MCBrowserViewControllerDelegate {
#IBOutlet weak var input: UITextField!
#IBOutlet weak var output: UILabel!
#IBAction func dick(_ sender: Any) {
output.text = input.text
UserDefaults.standard.set(input.text, forKey: "MyName")
input.text = ""
}
var currentPlayer:String!
var appDelegate:AppDelegate!
override func viewDidLoad() {
super.viewDidLoad()
appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.MPCHandler.setupPeerWithDisplayName(UIDevice.current.name)
appDelegate.MPCHandler.setupSession()
appDelegate.MPCHandler.advertiseSelf(true)
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.peerChangedStateWithNotification(_:)), name: NSNotification.Name(rawValue: "MPC_DidChangeStateNotification"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.handleReceivedDataWithNotification(_:)), name: NSNotification.Name(rawValue: "MPC_DidReceiveDataNotification"), object: nil)
}
#IBAction func connect(_ sender: Any) {
if appDelegate.MPCHandler.session != nil{
appDelegate.MPCHandler.setupBrowser()
appDelegate.MPCHandler.browser.delegate = self
self.present(appDelegate.MPCHandler.browser, animated: true, completion: nil)
}
}
#IBAction func action(_ sender: Any) {
output.text = input.text
UserDefaults.standard.set(input.text, forKey: "MyName")
input.text = ""
}
func peerChangedStateWithNotification(_ notification: Notification) {
let userInfo = notification.userInfo!
let state = userInfo["state"] as! Int
if state != MCSessionState.connecting.rawValue{
self.navigationItem.title = "Connected"
}
}
func handleReceivedDataWithNotification(_ notification: Notification) {
let userInfo = notification.userInfo!
print(userInfo)
}
func browserViewControllerDidFinish(_ browserViewController: MCBrowserViewController) {
appDelegate.MPCHandler.browser.dismiss(animated: true, completion: nil)
}
func browserViewControllerWasCancelled(_ browserViewController: MCBrowserViewController) {
appDelegate.MPCHandler.browser.dismiss(animated: true, completion: nil)
}
*func viewdidloadOverride; func viewDidAppear*(_ animated: Bool) {
if let x = UserDefaults.standard.string(forKey: "myName") {
output.text = x
}
else {
output.text = "x" //Set here default Name
}
self.createAlert(title: "HI", message: "ARE YOU READY")
}
func createAlert (title: String, message:String)
{
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: output.text, style: UIAlertActionStyle.default, handler: { (action) in
}))
self.present(alert,animated: true, completion:nil)
}
}

Perform a segue to a new viewController when clicking the "use photo" button after taking picture with camera iOS 10 (swift 3)

I want the user to be taken to a new viewController after clicking "use photo" after taking a picture. However, the segue is never performed. I have attempted this segue by connecting two viewControllers via a manual segue. I have a print statement in my imagePickerController function which is where my segue code is. This print statement prints when I click "use photo", so why is my segue be ignored? I tried this exact same segue code within a "#IBAction func someButton (_sender: UIButton)" and it worked fine? Any help would be appreciated.
Here is my code:
import UIKit
class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
#IBOutlet weak var imageTake: UIImageView!
var imagePicker: UIImagePickerController!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func takePhoto(_ sender: UIButton) {
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
present(imagePicker, animated: true, completion: nil)
}
#IBAction func photoLib(_ sender: UIButton) {
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
present(imagePicker, animated: true, completion: nil)
}
#IBAction func save(_ sender: AnyObject) {
UIImageWriteToSavedPhotosAlbum(imageTake.image!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
}
func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
if let error = error {
// we got back an error!
let ac = UIAlertController(title: "Save error", message: error.localizedDescription, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
} else {
let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
imagePicker.dismiss(animated: true, completion: nil)
imageTake.image = info[UIImagePickerControllerOriginalImage] as? UIImage
print("made it")
performSegue(withIdentifier: "new", sender: self)
}
}
Here's a suggestion. You have this:
imagePicker.dismiss(animated: true, completion: nil)
// ...
performSegue(withIdentifier: "new", sender: self)
Change it to this:
imagePicker.dismiss(animated: true, completion: {
performSegue(withIdentifier: "new", sender: self)
})
That way, we don't try to perform the segue until the image picker is in fact completely dismissed.
If that doesn't work, then I would have to conclude that there is no such segue as "new" coming from this view controller. But you have not shown your storyboard so I can't be certain.