Show image in imageview from gallery in swift - swift

I am new in IOS and i want to show image in imageView which is picked from gallery.And i have also added the photo description in info.plist.But image is not showing in imageView.Here is my code:
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate,UINavigationControllerDelegate {
#IBOutlet weak var img: UIImageView!
#IBOutlet weak var imageView: UIImageView!
#IBOutlet var chooseBuuton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func btnClicked() {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
private func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
//imageView.contentMode = .scaleAspectFit
img.image = pickedImage
}
self.dismiss(animated: true, completion: nil)
}
}

Assuming that you are using Swift 3.x:
You should implement imagePickerController(_:didFinishPickingMediaWithInfo:) as follows:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
//imageView.contentMode = .scaleAspectFit
img.image = pickedImage
}
self.dismiss(animated: true, completion: nil)
}
And it should be good to go. What are the differences? well:
1- Remove private.
2- The signature of the method is not the same, as mentioned in the documentation, info should be of type [String : Any] not [String : AnyObject].

Related

How to add 2 or more images into email from photo gallery to MFMailComposeViewController [duplicate]

I am making an app in which there are two UIImageViews. In each image view the user needs to be able to input a different image. Here is the code I have so far.
var imagePicker = UIImagePickerController()
#IBAction func chooseImage1(sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
println("Button capture")
imagePicker.delegate = self
imagePicker.sourceType = .SavedPhotosAlbum
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}
#IBAction func chooseImage2(sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
println("Button capture")
imagePicker2.delegate = self
imagePicker2.sourceType = .SavedPhotosAlbum
imagePicker2.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
chooseImage1.image = pickedImage
let pickedImage2 = info[UIImagePickerControllerOriginalImage] as? UIImage
chooseImage2.image = pickedImage2
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
This ends up picking the same image for each different image view. I would like to be able to choose two individual photos, one for each view. Thank you for your help.
You only need one UIImagePickerController. You can keep a reference of the tapped view and when the user finish picking the image, you just need to cast the selected view as UIImageView and set its image property:
update: Xcode 11.5 • Swift 5.2 or later
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate ,UINavigationControllerDelegate {
#IBOutlet weak var imageView1: UIImageView!
#IBOutlet weak var imageView2: UIImageView!
var imagePicker = UIImagePickerController()
var selectedVew: UIView!
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
imagePicker.sourceType = .savedPhotosAlbum
imagePicker.allowsEditing = false
[imageView1,imageView2].forEach {
$0?.isUserInteractionEnabled = true
$0?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(chooseImage)))
}
}
#objc func chooseImage(_ gesture: UITapGestureRecognizer) {
if UIImagePickerController.isSourceTypeAvailable(.savedPhotosAlbum) {
selectedVew = gesture.view
present(imagePicker, animated: true)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
(selectedVew as? UIImageView)?.image = info[.originalImage] as? UIImage
dismiss(animated: true)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true)
}
}

Use UIImagePicker to set the image in a UIImageView

This is how my code currently looks like. What I expect to happen is the profileImage (UIImageView) to display the selected image as currently it is not happening.
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet var profileImage: UIImageView!
let picker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
picker.sourceType = .photoLibrary
}
#IBAction func photoUploadPressed(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = .photoLibrary
self.present(myPickerController, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage{
selectedImage = image
profileImage.image = image
}
dismiss(animated: true, completion: nil)
}
}
Currently the Image Picker comes up, but once selected it does not change the UIImageView's image.
Any help will be much appreciated.
It is working as expected only error in your code is that you have } missing in photoUploadPressed(_ sender: Any) and you have not declared selectedImage & using it inside imagePickerController
Working code
import UIKit
class ViewController: UIViewController , UIImagePickerControllerDelegate, UINavigationControllerDelegate{
let picker = UIImagePickerController()
var selectedImage: UIImage?
#IBOutlet weak var profileImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
picker.delegate = self
picker.sourceType = .photoLibrary
}
#IBAction func potoUploadPressed(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = .photoLibrary
self.present(myPickerController, animated: true, completion: nil)
}// It is missing in your code
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage{
selectedImage = image // You have not declared selectedImage
profileImage.image = image
}
dismiss(animated: true, completion: nil)
}
}

I'm trying to display an image from the photo library in a UIImageView

The full error is:
2018-11-17 11:48:21.587818-0700 TestApp[3763:162426] [discovery]
errors encountered while discovering extensions: Error
Domain=PlugInKit Code=13 "query cancelled"
UserInfo={NSLocalizedDescription=query cancelled}
My code:
import UIKit
class CameraViewController: UIViewController {
#IBOutlet weak var captionTextView: UITextView!
#IBOutlet weak var photo: UIImageView!
#IBOutlet weak var shareButton: UIButton!
var selectedImage: UIImage?
override func viewDidLoad() {
super.viewDidLoad()
let tapGesturn = UITapGestureRecognizer(target: self, action: #selector(self.handleSelectPhoto))
photo.addGestureRecognizer(tapGesturn)
photo.isUserInteractionEnabled = true
}
#objc func handleSelectPhoto() {
let pickerController = UIImagePickerController()
pickerController.delegate = self
present(pickerController, animated: true, completion: nil)
}
#IBAction func shareButton_TouchUpInside(_ sender: Any) {
}
}
extension CameraViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
private func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
print("did Finish Picking Media")
if let image = info["UIImagePickerControllerOriginalImage"] as? UIImage{
selectedImage = image
photo.image = image
}
dismiss(animated: true, completion: nil)
}
}
Instead of using string "UIImagePickerControllerOriginalImage" as key for info dictionary use info key for original image UIImagePickerController.InfoKey.originalImage
So replace this
if let image = info["UIImagePickerControllerOriginalImage"] as? UIImage {
with this
if let image = info[.originalImage] as? UIImage {
also replace info dictionary type in parameters of delegate method from [String : Any] to [UIImagePickerController.InfoKey : Any]
imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
and last replace private keyword before delegate method with #objc

Choose photo from library in swift doesn't work

In my app I have a "addImgBtn" that should let the user to choose image from his photos. also I have "profileImg" that store some temporary image.
When I click the "addImg" button it shows me all photos, but when I click "Choose" it doesn't replace the temporary image with the chosen one, here my code:
class myClass: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
#IBOutlet weak var profileImg: UIImageView!
#IBOutlet weak var addImgBtn: UIButton!
#IBAction func addImgBtn(_ sender: Any) {
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
self.dismiss(animated: true, completion: { () -> Void in
})
profileImg.image = image
}
}
You are using the wrong imagePickerController method, change to the following:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
self.dismiss(animated: true, completion: { () -> Void in
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
self.profileImg.image = image
}
})
}
This should work! As a general tip, command-click into UIImagePickerControllerDelegate or any delegate you are implementing in xcode and it will tell you all available methods,etc.

how to show same picked image in two Image Views in Xcode swift 3?

I am unable to figure out how to use picked image and show it in two image view at the same time. Basically, I want user to click on button then select an image and after that the image is shown in two different image views( Using Swift 3 and Xcode 8 ).
#IBOutlet var imageview: UIImageView!
var imagePicker = UIImagePickerController()
#IBOutlet weak var effectView: UIVisualEffectView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
imageview.image = UIImage(named:"Selfie_4072_5194.jpg")
imagePicker.delegate = self
imageview2.image = imageview.image
imagePicker.delegate = self
}
#IBAction func camerabutton(_ sender: UIButton) {
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.allowsEditing = true
present(imagePicker, animated: true, completion: nil)
}
#IBAction func libraryBtn(_ sender: AnyObject) {
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePicker.allowsEditing = true
present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
imageview.image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.dismiss(animated: true, completion: {})
}
#IBAction func Screenshot(_ sender: UIButton) {
I can't see where you are defining imageview2 make sure you have added it as an outlet, and linked it in your storyboard.
#IBOutlet var imageview2: UIImageView!
It also looks like you are only setting the image for imageview try setting the image property of your imageview2 too.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
imageview.image = image
imageview2.image = image
}
self.dismiss(animated: true, completion: {})
}