i wrote a function, that takes an enumeration value as argument. I don't get any errors for the function prototype, but as soon as i try to call the function an an block, ill get an error saying "Use of unresolved identifier":
#IBAction func addButtonPressed(sender: AnyObject) {
let actionMenu = UIAlertController(title: "Take a Photo or choose an
existing one", message: nil, preferredStyle:
UIAlertControllerStyle.ActionSheet)
let takePhotoAction = UIAlertAction(title: "take Photo", style: UIAlertActionStyle.Default, handler: {(alert: UIAlertAction!) -> Void in
println("take Photo")
})
let choosePhotoAction = UIAlertAction(title: "choose an existing Photo", style: UIAlertActionStyle.Default, handler: {(alert: UIAlertAction!) -> Void in
println("choose Photo")
//getting error right here
self.displayPickerController(UIImagerPickerControllerSourceType.PhotoLibrary)
})
actionMenu.addAction(takePhotoAction)
actionMenu.addAction(choosePhotoAction)
presentViewController(actionMenu, animated: true, completion: nil)
}
// MARK: - Helper Methods
func displayPickerController(withSource: UIImagePickerControllerSourceType){
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = withSource
presentViewController(imagePicker, animated: true, completion: nil)
}
You have a typo in self.displayPickerController(UIImagerPickerControllerSourceType.PhotoLibrary). You typed UIImager instead of UIImage when naming the enum.
Related
I'm new to swift and I'm trying to add a picture from the gallery/camera to an image view and also pass it backward and store it in the array. Below is what I have so far I can click the button and show me an action sheet that takes me to the gallery and the camera just don't know how to assign it to my imageView and pass the image info back.
#IBAction func addPhoto(_ sender: Any) {
showSimpleActionSheet(controller: self)
}
func showSimpleActionSheet(controller: UIViewController) {
let alert = UIAlertController(title: "Choose an image", message: "", preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (_) in
self.getImage(fromSourceType: .camera)
}))
alert.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: { (_) in
self.getImage(fromSourceType: .photoLibrary)
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (_) in
}))
self.present(alert, animated: true, completion: {
})
}
extension NewViewController: UIImagePickerControllerDelegate,UINavigationControllerDelegate {
func getImage(fromSourceType sourceType: UIImagePickerController.SourceType){
if UIImagePickerController.isSourceTypeAvailable(sourceType){
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.sourceType = sourceType
self.present(imagePickerController, animated: true, completion: nil)
} else {
print("Source type isn't available")
}
}
}
When you create an image picker controller, you have to set a delegate. That delegate must conform to the UIImagePickerControllerDelegate protocol.
The image picker controller will invoke it’s delegate’s methods to notify it when the user picks an image/movie. That’s where you would accept the user-selected image and install it into your image view (or whatever it is you want to do with the image).
The delegate method that tells you what media the user selected is imagePickerController(_:didFinishPickingMediaWithInfo:).
Show us your implementation of that method.
In iOS Swift 3.1 I'm trying to access the camera, the same way I have done successfully in other apps. However, in this one particular app, it always crashes on the self.present(imagePicker, animated: true, completion: nil) line. The message in the console is Message from debugger: Terminated due to signal 9. Does this usually signal a memory related error?
#IBAction func onChooseImageClick(_ sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.savedPhotosAlbum){
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
//Create the AlertController and add Its action like button in Actionsheet
let actionSheetControllerForImage: UIAlertController = UIAlertController(title: "Please select", message: "Option to select", preferredStyle: .actionSheet)
let cancelActionButton: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in
print("Cancel")
}
actionSheetControllerForImage.addAction(cancelActionButton)
let cameraActionButton: UIAlertAction = UIAlertAction(title: "Camera", style: .default)
{ action -> Void in
print("Camera")
if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)) {
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
let mediaTypes:[String] = [kUTTypeImage as String]
imagePicker.mediaTypes = mediaTypes
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
} else {
let alertController = UIAlertController(title: "error", message: "Camera not found!", preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .cancel) { action in
// ...
}
alertController.addAction(OKAction)
self.present(alertController, animated: true)
}
}
actionSheetControllerForImage.addAction(cameraActionButton)
let galleryActionButton: UIAlertAction = UIAlertAction(title: "Image Gallery", style: .default)
{ action -> Void in
imagePicker.sourceType = UIImagePickerControllerSourceType.savedPhotosAlbum;
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
actionSheetControllerForImage.popoverPresentationController?.sourceView = self.view
actionSheetControllerForImage.addAction(galleryActionButton)
===> self.present(actionSheetControllerForImage, animated: true, completion: nil)
}
}
You need a string in your plist file that explains why your app need permission to use the camera. There's a great summary of these strings in the question at iOS 10 - Changes in asking permissions of Camera, microphone and Photo Library causing application to crash, with a list you can copy from in the answer at https://stackoverflow.com/a/40734360/968577
Without the required strings, your app will crash in this manner. However, the console output will explain what the problem is.
Try like this i hope it would be helpful!!
Add these two points in info.plist first
Privacy - Camera Usage Description
Privacy - Photo Library Usage Description
Add these two delegates in your class file
UIImagePickerControllerDelegate
UINavigationControllerDelegate
#IBAction func onclickImageAction(_ sender: Any){
print("onclickImageAction method called here")
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.isEditing = false
let actionSheet = UIAlertController(title: "Select Profile Photo", message: "", preferredStyle: UIAlertControllerStyle.actionSheet)
let libButton = UIAlertAction(title: "Select photo from library", style: UIAlertActionStyle.default){ (libSelected) in
print("library selected")
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
self.present(imagePicker, animated: true, completion: nil)
}
actionSheet.addAction(libButton)
let cameraButton = UIAlertAction(title: "Take a picture", style: UIAlertActionStyle.default) { (camSelected) in
if (UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera))
{
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
self.present(imagePicker, animated: true, completion: nil)
}
else
{
actionSheet.dismiss(animated: false, completion: nil)
let alert = UIAlertController(title: "Error", message: "There is no camera available", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Okay", style: .default, handler: { (alertAction) in
alert.dismiss(animated: true, completion: nil)
}))
}
}
actionSheet.addAction(cameraButton)
let cancelButton = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) { (cancelSelected) in
print("cancel selected")
}
actionSheet.addAction(cancelButton)
let albumButton = UIAlertAction(title: "Saved Album", style: UIAlertActionStyle.default) { (albumSelected) in
print("Album selected")
imagePicker.sourceType = UIImagePickerControllerSourceType.savedPhotosAlbum
self.present(imagePicker, animated: true, completion: nil)
}
actionSheet.addAction(albumButton)
self.present(actionSheet, animated: true, completion:nil)
}
Implement these delegate method
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{if let PickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
{
yourimageview.image = PickedImage
dismiss(animated: true, completion: nil)
}
}
I just added exit(0) when i opened the camera settings. It's working fine
I am using an alert to perform a Segue to another view.
let defaultAction = UIAlertAction(title: "Proceed", style: .Default, handler: { (UIAlertAction) -> Void in
performSegueWithIdentifier("proceed", sender: self)
})
I just can't figure out why I get the error:
Extra argument 'sender' in call
The sender should be just self, right?
This error is driving me crazy for hours now, I can't figure out what's wrong!
Any help is appreciated! Thanks!
Try this code :
override func viewDidLoad() {
super.viewDidLoad()
self.showAlert() //Calling function
}
func showAlert()
{
let alertController = UIAlertController(title: "Download Complete", message: "The list of user IDs has been downloaded. Proceed to know who viewed your profile.", preferredStyle: .Alert)
let defaultAction = UIAlertAction(title: "Proceed", style: .Default, handler: { (UIAlertAction) -> Void in
self.performSegueWithIdentifier("proceed", sender: self)
})
alertController.addAction(defaultAction)
self.presentViewController(alertController, animated: true, completion:nil)
}
So this is my full code:
let alertController = UIAlertController(title: "Download Complete", message: "The list of user IDs has been downloaded. Proceed to know who viewed your profile.", preferredStyle: .Alert)
let defaultAction = UIAlertAction(title: "Proceed", style: .Default, handler: { (UIAlertAction) -> Void in
performSegueWithIdentifier("proceed", sender: nil)
})
override func viewDidLoad() {
super.viewDidLoad()
self.alertController.addAction(self.defaultAction)
self.presentViewController(self.alertController, animated: true, completion: nil)
}
try this
let alert = UIAlertView()
alert.title = "Download Complete"
alert.message = "he list of user IDs has been downloaded. Proceed to know who viewed your profile."
alert.addButtonWithTitle("ok")
alert.show()
var okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
UIAlertAction in
NSLog("OK Pressed")
performSegueWithIdentifier("proceed", sender: nil)
}
alertController.addAction(okAction)
if (view.annotation.title as String!) == "Helgoland " {
currentLong = 7.889021
currentLat = 54.180210
url = "google.com"
let alertController: UIAlertController = UIAlertController(title: "Change Map Type", message: nil, preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction: UIAlertAction = UIAlertAction(title: "Back", style: UIAlertActionStyle.Cancel, handler: nil)
let button1action: UIAlertAction = UIAlertAction(title: "Product Page", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) -> () in
performSegueWithIdentifier("showShop", sender: self)
})
let button2action: UIAlertAction = UIAlertAction(title: "Video", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) -> () in
youtubeVideoID = "XX"
UIApplication.sharedApplication().openURL(NSURL(string: "http://www.youtube.com/watch?v=\(youtubeVideoID)"))
})
alertController.addAction(cancelAction)
alertController.addAction(button1action)
alertController.addAction(button2action)
self.presentViewController(alertController, animated: true, completion: nil)
}
I always get an error with
"Implicit use of 'self' in closure; use 'self.' to make capture
semantic explicit"
but if I set self.view, it also fails.
You'll need to explicitly use self :
self.performSegueWithIdentifier("showShop", sender: self)
And for Swift 3 (thx #KingChintz) :
self.performSegue(withIdentifier: "showShop", sender: self)
For Swift 3
_ = self.navigationController?.popToRootViewController(animated: false)
I'm trying to build an app using ELCImagePickerController. I found that I could select multiple pictures. However, the ELCImagePickerController delegate method was not called.
This is my code:
#IBAction func uploadImages(sender: AnyObject) {
// Create the alert controller
//var alertController = UIAlertController(title: "", message: "", preferredStyle: .Alert)
var alertController = UIAlertController(title: nil, message: nil, preferredStyle: .Alert)
// Create the actions
var takeAction = UIAlertAction(title: "Take Photos", style: UIAlertActionStyle.Default) {
UIAlertAction in
NSLog("Take Photos Pressed")
}
var selectAction = UIAlertAction(title: "Select Photos", style: UIAlertActionStyle.Default) {
UIAlertAction in
NSLog("Select photos Pressed")
var imagePicker = ELCImagePickerController(imagePicker: ())
imagePicker.maximumImagesCount = 2
imagePicker.returnsOriginalImage = false
imagePicker.returnsImage = true
imagePicker.onOrder = true
imagePicker.delegate = self
self.presentViewController(imagePicker, animated: true, completion: nil)
}
var cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) {
UIAlertAction in
NSLog("Cancel Pressed")
}
// Add the actions
alertController.addAction(takeAction)
alertController.addAction(selectAction)
alertController.addAction(cancelAction)
// Present the controller
self.presentViewController(alertController, animated: true, completion: nil)
}
}
func elcImagePickerController(picker: ELCImagePickerController!, didFinishPickingMediaWithInfo info:[AnyObject]!) {
NSLog("controller executed.")
}
You need to set the ImagePicker delegate
imagePicker.imagePickerDelegate = self
Are your NSLog statements ever getting called? One thing I notice in your trailing closure syntax is that you're using the type name versus a variable of that type. For instance, you're writing UIAlertAction in ... vs alertAction in .... You should be providing a name to be used within the closure rather than the type itself. If the rest of the closure is not executing, then the delegate is never being set, and therefore delegate methods are never called.