I am having a problem displaying the photo I just took or chose to the image view.
Below is a pic of the imagview on the left and the licencePhoto button on right.
When I click on Take Photo, it allows me to take a pic or select from my library, as the code shows:
#IBAction func seclectOrTakePhoto(_ sender: Any) {
pickerController.delegate = self
pickerController.allowsEditing = true
let alertController = UIAlertController(title: "Add a Picture", message: "Choose From", preferredStyle: .actionSheet)
let cameraAction = UIAlertAction(title: "Camera", style: .default) { (action) in
self.pickerController.sourceType = .camera
self.present(self.pickerController, animated: true, completion: nil)
}
let photosLibraryAction = UIAlertAction(title: "Photos Library", style: .default) { (action) in
self.pickerController.sourceType = .photoLibrary
self.present(self.pickerController, animated: true, completion: nil)
}
let savedPhotosAction = UIAlertAction(title: "Saved Photos Album", style: .default) { (action) in
self.pickerController.sourceType = .savedPhotosAlbum
self.present(self.pickerController, animated: true, completion: nil)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: nil)
alertController.addAction(cameraAction)
alertController.addAction(photosLibraryAction)
alertController.addAction(savedPhotosAction)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
}
But, it does not display the photo to the licensePhoto image and in Firebase, it is uploading the pic I have displayed in the image view as below and not the one I just took with phone.
How can I achieve this?
Edited
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
self.licensePhoto.image = image
self.dismiss(animated: true, completion: nil)
// any time the photo changes, check the button status
updateSignupButtonStatus()
}
After searching and looking through tutorials, I finally got something to work:
#objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
licensePhoto.contentMode = .scaleAspectFit
licensePhoto.image = chosenImage
pickerController.dismiss(animated: true, completion: nil)
}
Related
I am successful at taking or picking a photo and uploading to Firebase Storage but I am not sure how to set that pic to the UIImage, see code:
UIImage that needs the photo to be set to:
#IBOutlet weak var myPhoto: UIImageView!
How to select or take a photo:
imagePicker.allowsEditing = true
let alertController = UIAlertController(title: "Add a Photo", message: "Choose From", preferredStyle: .actionSheet)
let cameraAction = UIAlertAction(title: "Camera", style: .default) { (action) in
self.imagePicker.sourceType = .camera
self.imagePicked = sender.tag // new
self.present(self.imagePicker, animated: true, completion: nil)
}
let photosLibraryAction = UIAlertAction(title: "Photos Library", style: .default) { (action) in
self.imagePicker.sourceType = .photoLibrary
self.imagePicked = sender.tag // new
self.present(self.imagePicker, animated: true, completion: nil)
}
let savedPhotosAction = UIAlertAction(title: "Saved Photos Album", style: .default) { (action) in
self.imagePicker.sourceType = .savedPhotosAlbum
self.imagePicked = sender.tag // new
self.present(self.imagePicker, animated: true, completion: nil)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: nil)
alertController.addAction(cameraAction)
alertController.addAction(photosLibraryAction)
alertController.addAction(savedPhotosAction)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
How do I set the photo I just selected or took to myPhoto?
You can get selected or captured image from
UIImagePickerControllerDelegate function. You must need to set
delegate of your UIImagePickerController(picker) instance.
Asking for permission
The app must ask for permission from the user before accessing the
camera/ the saved photos.The app should display a message to the user
explaining why it needs the camera or photo library access. You can
set this message by setting the NSCameraUsageDescription and
NSPhotoLibraryUsageDescription key in the Info.plist file of your app.
This will work for the sure
imagePicker.delegate = self
extension StackOverflowViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController( picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
var selectedImage: UIImage?
if let editedImage = info[.editedImage] as? UIImage {
selectedImage = editedImage
self.imgView.image = selectedImage!
} else if let originalImage = info[.originalImage] as? UIImage {
selectedImage = originalImage
self.imgView.image = selectedImage!
picker.dismiss(animated: true, completion: nil)
}
}
func imagePickerControllerDidCancel( picker: UIImagePickerController) {
picker.dismiss(animated: true) {
// Further logic to perform
}
}
}
You can review all other related to UIImagePickerController from this
official reference link.
https://developer.apple.com/documentation/uikit/uiimagepickercontroller/infokey/1619164-originalimage
I am using a UIImagePickerController in my program and it is effectively changing the image of an imageview i have added. However, whenever I restart this app and come back to the home screen, it is automatically resetting to the default image I had it to before, rather than the user selected image. How can I make it so that it records which image was last used, and reloads it every time the program starts?
var imagePicker = UIImagePickerController()
func chooseImage(_ sender: Any) { //function called with button press
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.allowsEditing = true
let actionSheet = UIAlertController(title: "Photo Source", message: "Choose a source", preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (action:UIAlertAction) in
if UIImagePickerController.isSourceTypeAvailable(.camera) {
imagePickerController.sourceType = .camera
self.present(imagePickerController, animated: true, completion: nil)
}else{
print("Camera not available")
}
}))
actionSheet.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: { (action:UIAlertAction) in
imagePickerController.sourceType = .photoLibrary
self.present(imagePickerController, animated: true, completion: nil)
}))
actionSheet.addAction(UIAlertAction(title: "Default", style: .default, handler: { (action:UIAlertAction) in
self.avatarImageView.image = UIImage(named: "Avatar.png")
}))
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(actionSheet, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerEditedImage] as! UIImage
avatarImageView.image = image
picker.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
Since the app is going out of memory, you'll need some kind of persistence mechanism for saving the image. The simplest way to do this would be to store the image in UserDefaults. This can be accomplished like this:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerEditedImage] as! UIImage
avatarImageView.image = image
UserDefaults.standard.set(UIImagePNGRepresentation(image), forKey: "avatarImage")
picker.dismiss(animated: true, completion: nil)
}
Then when you reopen the app you'll need to check whether you've previously saved an avatarImage in UserDefaults and load it from there:
// Could be in viewDidLoad or wherever else you load your image
override func viewDidLoad() {
if let imageData = UserDefaults.standard.object(forKey: "avatarImage") as? Data {
avatarImageView.image = UIImage(data: imageData)
}
}
Image Gallery not open . Camera working fine but image getting from Photo Library Not getting they show blank screen. Yesterday it working Fine But today it not open . Please help any help would be apprciated. I am newer in Ios
#IBAction func uploadImageClick (sender : UIButton){
let optionMenu = UIAlertController(title: nil, message: "Choose Option", preferredStyle: .ActionSheet)
let deleteAction = UIAlertAction(title: "Choose From Gallery", style: .Default, handler: {
(alert: UIAlertAction!) -> Void in
self.picker.allowsEditing = true
self.picker.sourceType = .PhotoLibrary
self.presentViewController(self.picker, animated: true, completion: nil)
})
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: {
(alert: UIAlertAction!) -> Void in
})
optionMenu.addAction(deleteAction)
optionMenu.addAction(cancelAction)
self.presentViewController(optionMenu, animated: true, completion: nil)
}
extension NewsViewController : UIImagePickerControllerDelegate,UINavigationControllerDelegate{
func imagePickerController(
picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : AnyObject]){
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
dismissViewControllerAnimated(true, completion: nil)
}
}
You need to add below thing in plist
Key is Privacy - Photo Library Usage Description
Type is String
Value is uses Photos(Whatever you want give here)
This question already has an answer here:
Picking two different images in the same view controller using imagePickerController in Swift
(1 answer)
Closed 6 years ago.
I have two UIImageViews, each with two buttons. One button takes a picture and the other chooses a photo from a library. Both buttons work correctly. But when I choose an image by a button from the first sheet, it is displayed in both UIImageViews. My question is how can we display the image in only the corresponding ImageView?.
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [NSObject : AnyObject]?) {
print("Image Selected")
self.dismissViewControllerAnimated(true, completion: nil)
importedImage.image = image
secondPhoto.image = image
}
// MARK: - Action Sheet
#IBAction func showActionSheet1(sender: AnyObject) {
let image = UIImagePickerController()
image.delegate = self
let actionSheet = UIAlertController(title: "Action Sheet", message: "Choose Option", preferredStyle: .ActionSheet)
let libButton = UIAlertAction(title: "Select from photo library", style: .Default, handler: { (libButton) -> Void in
image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
image.allowsEditing = false
self.presentViewController(image, animated: true, completion: nil)
})
let cameraButton = UIAlertAction(title: "Take picture", style: .Default, handler: { (cameraButton) -> Void in
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){
image.sourceType = UIImagePickerControllerSourceType.Camera
image.allowsEditing = false
self.presentViewController(image, animated: true, completion: nil)
}
})
let cancelButton = UIAlertAction(title: "Cancel", style: .Cancel, handler: {(cancelButton) -> Void in
print("Cancel selected")
})
actionSheet.addAction(libButton)
actionSheet.addAction(cameraButton)
actionSheet.addAction(cancelButton)
self.presentViewController(actionSheet, animated: true, completion: nil)
}
#IBAction func showActionSheet2(sender: AnyObject) {
let image = UIImagePickerController()
image.delegate = self
let actionSheet = UIAlertController(title: "Action Sheet", message: "Choose Option", preferredStyle: .ActionSheet)
let libButton = UIAlertAction(title: "Select from photo library", style: .Default, handler: { (libButton) -> Void in
image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
image.allowsEditing = false
self.presentViewController(image, animated: true, completion: nil)
})
let cameraButton = UIAlertAction(title: "Take picture", style: .Default, handler: { (cameraButton) -> Void in
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){
image.sourceType = UIImagePickerControllerSourceType.Camera
image.allowsEditing = false
self.presentViewController(image, animated: true, completion: nil)
}
})
let cancelButton = UIAlertAction(title: "Cancel", style: .Cancel, handler: {(cancelButton) -> Void in
print("Cancel selected")
})
actionSheet.addAction(libButton)
actionSheet.addAction(cameraButton)
actionSheet.addAction(cancelButton)
self.presentViewController(actionSheet, animated: true, completion: nil)
}
This is happening because you are setting image in both the ImageView inside didFinishPickingImage, to solve the issue need to maintain some state like which Button is clicked to set Image, for that you can create one Bool instance and assign its value inside the action of Button like this.
var isFromFirst: Bool = false
#IBAction func showActionSheet1(sender: AnyObject) {
self.isFromFirst = true
...//your other code
}
#IBAction func showActionSheet2(sender: AnyObject) {
self.isFromFirst = false
...//your other code
}
After that check this bool value inside didFinishPickingImage like this.
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [NSObject : AnyObject]?) {
print("Image Selected")
self.dismissViewControllerAnimated(true, completion: nil)
if (self.isFromFirst) {
importedImage.image = image
}
else {
secondPhoto.image = image
}
}
I am creating an app that utilises this image picker code retrieved from: https://github.com/projectwakii/UIImagePickerController-in-Swift
Shown below:
#IBAction func imageButtonDidPress(sender: AnyObject) {
print("pressed")
//show the action sheet (i.e. the little pop-up box from the bottom that allows you to choose whether you want to pick a photo from the photo library or from your camera
let optionMenu = UIAlertController(title: nil, message: "Where would you like the image from?", preferredStyle: UIAlertControllerStyle.ActionSheet)
let photoLibraryOption = UIAlertAction(title: "Photo Library", style: UIAlertActionStyle.Default, handler: { (alert: UIAlertAction!) -> Void in
print("from library")
//shows the photo library
self.imagePicker.allowsEditing = true
self.imagePicker.sourceType = .PhotoLibrary
self.imagePicker.modalPresentationStyle = .Popover
self.presentViewController(self.imagePicker, animated: true, completion: nil)
})
let cameraOption = UIAlertAction(title: "Take a photo", style: UIAlertActionStyle.Default, handler: { (alert: UIAlertAction!) -> Void in
print("take a photo")
//shows the camera
self.imagePicker.allowsEditing = true
self.imagePicker.sourceType = .Camera
self.imagePicker.modalPresentationStyle = .Popover
self.presentViewController(self.imagePicker, animated: true, completion: nil)
})
let cancelOption = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: {
(alert: UIAlertAction!) -> Void in
print("Cancel")
self.dismissViewControllerAnimated(true, completion: nil)
})
//Adding the actions to the action sheet. Camera will only show up as an option if the camera is available in the first place.
optionMenu.addAction(photoLibraryOption)
optionMenu.addAction(cancelOption)
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) == true {
optionMenu.addAction(cameraOption)} else {
print ("I don't have a camera.")
}
self.presentViewController(optionMenu, animated: true, completion: nil)
/*
just adding extra text for fun
*/
}
// MARK: - Image Picker Delegates
//The UIImagePickerController is a view controller that gets presented modally. When we select or cancel the picker, it runs the delegate, where we handle the case and dismiss the modal.
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
print("finished picking image")
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
//handle media here i.e. do stuff with photo
print("imagePickerController called")
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
imageButtonImage.image = chosenImage
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
//what happens when you cancel
//which, in our case, is just to get rid of the photo picker which pops up
dismissViewControllerAnimated(true, completion: nil)
}
The issue is that most of the times that I select an image the app crashes.I think it is partially due to some sort of low memory warning, although I am not sure. It is after I click the "use photo" button as shown below
Also, does anybody have any idea how I can remove the square thing whenever I take the photo as visible above
Thanks in advance!
Try using:
self.imagePicker.allowsEditing = false