ELCImagePickerController does not back to viewController in swift - ios

I useELCImagePickerController to select multiple photos. However when i select photos and click Done button its goes back to select an album page. Please help me so when i select photos it should back to viewController .
Here is code i using:
var picker = ELCImagePickerController(imagePicker: ())
#IBAction func ButtonIsclick(sender: AnyObject) {
picker.delegate = self
self.presentViewController(picker, animated: true, completion: nil)
}
func elcImagePickerController(picker: ELCImagePickerController!, didFinishPickingMediaWithInfo info:[AnyObject]!) {
self.dismissViewControllerAnimated(true, completion: nil)
}
func elcImagePickerControllerDidCancel(picker: ELCImagePickerController!){
self.dismissViewControllerAnimated(true, completion: nil)
}
EDIT: When i debug the code its never call the didFinishPickingMediaWithInfo function

Actually i face this problem because of wrongly set the delegate.
In my question i set delegate as
picker.delegate = self
Which is wrong . Right way is to set ELCImagepickerDelegate is
picker.imagePickerDelegate = self

I solved it - find below the complete final working code -
The problem was I had to add ELCimagepickerdelegate to the class to be as:
class ViewController: UIViewController, UINavigationControllerDelegate, ELCImagePickerControllerDelegate {
however I used to get an error (Type viewcontroller does not conform to protocol) every time I do this, so the solution was to ignore this error until I add the 2 delegate methods below in my code (that stopped the error, which was very confusing - sorry I am new to swift. thanks to everybody tried to help
Whole working Code:
import UIKit
import ELCImagePickerController
class ViewController: UIViewController, UINavigationControllerDelegate, ELCImagePickerControllerDelegate {
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.
}
var picker = ELCImagePickerController()
#IBAction func OpenPhotos(_ sender: AnyObject) {
picker.imagePickerDelegate = self
self.present(picker, animated: true, completion: nil)
}
func elcImagePickerController(_ picker: ELCImagePickerController!, didFinishPickingMediaWithInfo info: [Any]!) {
dismiss(animated: true, completion: nil)
}
func elcImagePickerControllerDidCancel(_ picker: ELCImagePickerController!) {
dismiss(animated: true, completion: nil)
}
}

Related

UIImagePickerController to import Image

i want import image in ios from library but i have error
value of type 'viewcontroller' has no member 'present'
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func Alert(sender: AnyObject) {
let nextController = UIImagePickerController()
self.present(nextController, animated: true, completion: nil)
}}
Here's my stripped working code. I use two pickers - one for camera, one for library - because of some odd iOS bug with tint color. To avoid "massive view controller" issues, I tend to move everything possible related to a a UIImagePickerController into a separate file and extension.
class OpenViewController: UIViewController {
let pickerLibrary = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
pickerLibrary.delegate = self
}
}
extension OpenViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#objc func showImagePicker() {
pickerLibrary.allowsEditing = false
pickerLibrary.sourceType = .photoLibrary
present(pickerLibrary,
animated: true,
completion: nil)
pickerLibrary.popoverPresentationController?.sourceView = self.view
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: false, completion: nil)
}
}
While the present code looks correct, I notice several things you didn't post in your code. Maybe because you just didn't post it, but I would definitely check these things:
Are you setting ViewController to be a delegate?
Are you setting ViewController.view to be the source view? This may not be needed for iPhone, but definite helps with iPad.
Have you includes everything needed to be working for UIImagePickerControllerDelegate, UINavigationControllerDelegate?
Now as your error suggests, something else is likely the issue - a UIViewController can present another view controller. In this case you've given us no way to duplicate that error. In other words, I'm assuming that (1) You are seeing a UIButton attached to Alert() - BTW, Swift conventions use lower case for that - and (2) You've proven that when that button is tapped, `Alert() is actually executed.

Function created, but never called, yet it is still executed

I am very new to iOS development and Swift, so sorry if this is a trivial question. I have the following code
import UIKit
class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
var imagePicker: UIImagePickerController!
#IBOutlet weak var imageView: UIImageView!
#IBAction func openGalery(_ sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary;
imagePicker.allowsEditing = true
present(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
imagePicker.dismiss(animated: true, completion: nil)
imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}
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.
}
}
In the code I create the func imagePickerController. imagePickerController is responsible for setting the selected image in the imageView. It works. I open the galery, I select an image and there is a transition back to the view with the image set. However, why is it working? I never call the function imagePickerController. Comparing this with the languages I worked before, I should call this function on tap of the select button in the PhotoLibrary. Like an event or something. But here it is somehow called automatically, only by defining the function. Why is that?
imagePicker.delegate = self
Translates to: "Hey imagePicker, when something happens, let me know by calling my UIImagePickerControllerDelegate methods!"
In particular, your implementation of imagePickerController(_:didFinishPickingMediaWithInfo:) is being called, which is part of the UIImagePickerControllerDelegate protocol.
It's delegation pattern.
When you set imagePicker's delegate to self, you're starting receiving events from picker.
Events defined in protocol. In this case it's UIImagePickerControllerDelegate
This pattern frequently using in iOS developing.
More about delegation pattern you can read in Apple's documentation

Selecting photo returns to blank UI

I am trying to make an image uploader on my ios app.
EDIT 7
This question is now more clearly asked here: Present Modal for Image Select Erases UI
END OF EDIT 7
The first thing I am trying to do is enable the user to select an image that they want for upload.
EDIT 6
I have simplified all the code to simply one button press. The Modal presentation deletes the previous page's UI. How I prevent this.
import UIKit
class AddImageToStandViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func loadImageButtonTapped(sender: UIButton) {
let imagePicker = UIImagePickerController()
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}
I have found that the selection works just fine and does not destroy the UI if I push/segue from a different to this controller.
END OF EDIT 6
Below this is the other methods that I have tried before this
So far, this code enables a user to press a button and choose a photo from their photos. But, after they click the photo it just returns them to an empty View except the navbar at the bottom.
It seems my user is being returned, not to the view that they came from, but to a separate empty view. How do I return the user to the view that they came from?
import UIKit
class CreateStandViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet var image: UIImageView!
...
#IBAction func selectPicture(sender: AnyObject) {
let ImagePicker = UIImagePickerController()
ImagePicker.delegate = self
ImagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(ImagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
image.image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.dismissViewControllerAnimated(true, completion: nil)
}
...
let me know if there is any more information that I should give.
EDIT 1
this code has the same effect
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
image.image = info[UIImagePickerControllerOriginalImage] as? UIImage
picker.dismissViewControllerAnimated(true, completion: nil)
}
I have tried adding this line below but I get an error stating that CreateStandViewController.Type cannot be converted to the expected UIViewController.
self.presentViewController(CreateStandViewController, animated: true, completion: nil)
EDIT 2
This code below doesnt error but also changes nothing
let createStandController = CreateStandViewController()
self.presentViewController(createStandController, animated: true, completion: nil)
EDIT 3
I have literally copied all the code from here: http://www.codingexplorer.com/choosing-images-with-uiimagepickercontroller-in-swift/ and now have:
import UIKit
class CreateStandViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet var imageView: UIImageView!
let imagePicker = UIImagePickerController()
#IBAction func loadImageButtonTapped(sender: UIButton) {
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary
presentViewController(imagePicker, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
}
// MARK: - UIImagePickerControllerDelegate Methods
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
imageView.contentMode = .ScaleAspectFit
imageView.image = pickedImage
}
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
}
This has the same issue as before. When the image is selected I am sent back to a view that is white with the navbar at the bottom.
EDIT 4
I have run the same code in its own separate project as a stand alone view controller and it works. There is some code that I have in the background of my app that is killing the functionality of the code (maybe). I am guessing it may have something to do with the navbar?
EDIT 5
I removed the tab bar at the bottom by seguing to the image selecting view via a present Modally segue, but it had no effect.
Just replace your
self.dismissViewControllerAnimated(true, completion: nil)
with
picker.dismissViewControllerAnimated(true, completion: nil)
I think you will get the point after that

Importing Camera Roll Image to UIView background

A small warning: I'm a complete noob at swift
Im creating a small app that (will eventually) allow a user to draw over an image they choose from their camera roll on their iPhone. However, im having some trouble getting the image to be set as the background to the UIView (drawView). Could someone give me some pointers as to how I could go about this? The code I have scraped together is shown below.
My ViewController File:
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet var drawView : AnyObject!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
imagePicker.delegate = self
#IBAction func clearTapped() {
var theDrawView: DrawView = drawView as! DrawView
theDrawView.lines = []
theDrawView.setNeedsDisplay()
}
let imagePicker = UIImagePickerController()
#IBAction func importImage(sender: AnyObject) {
imagePicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
imagePicker.allowsEditing = true
self.presentViewController(imagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]){
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
drawView.backgroundImage = pickedImage
}
func dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
}
}
Also, my drawView has a separate swift file that deals with how the lines are drawn in it by the user.
you're going to want to separate your code a little more. right now, it seems like you're trying to create a controller within a controller. implement a class that is a subclass of UIIMagePickerController with the necessary delegate (you already have the code, so just copy and paste):
class MyImagePicker: UIImagePickerController, UIImagePickerControllerDelegate {
}
then in your view controller, initialize your image picker:
let myImagePicker = MyImagePicker()
call the correct method to return the image, set the background image of your UIView to that image, and then add that view as a subview:
drawView.backgroundImage = pickedImage
self.addSubview(pickedImage)
hope that works!

How to launch camera on viewDidLoad with UIImagePickerControllerDelegate?

I have a new iOS app in Swift where I'm trying to show the camera as soon as the app is launched. In my view controller I have a single UIView object which I try to fill with the camera.
The code in my view controller calls UIImagePickerController in the viewDidLoad method but nothing happens.
Here is the code, any idea why nothing happens? Isn't the camera supposed to open on app launch with this code?
import UIKit
class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
let imagePicker = UIImagePickerController()
#IBOutlet weak var imageViewer: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
if UIImagePickerController.isCameraDeviceAvailable( UIImagePickerControllerCameraDevice.Front) {
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
presentViewController(imagePicker, animated: true, completion: nil)
} else {
println("no front camera available")
}
// 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.
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
dismissViewControllerAnimated(true, completion: nil)
imageViewer.image = image
}
}
UPDATE: if I add a button to the storyboard, connect it to the code and put the code from viewDidLoad in it, the camera works when the button is clicked:
#IBAction func presentImagePicker(sender: AnyObject) {
if UIImagePickerController.isCameraDeviceAvailable( UIImagePickerControllerCameraDevice.Front) {
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
presentViewController(imagePicker, animated: true, completion: nil)
}
}
I know that the camera works, but how to make it work without having to press a button?
Here is the solution for you.
Put your code in viewDidAppear method this way:
override func viewDidAppear(animated: Bool) {
if UIImagePickerController.isCameraDeviceAvailable( UIImagePickerControllerCameraDevice.Front) {
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
presentViewController(imagePicker, animated: true, completion: nil)
}
}
This will load camera when your app start.
Trying to do it in viewDidLoad won't work because that is called after the view is first created, and the view isn't a subview of anything else at that point.
override func viewDidLoad() {
if UIImagePickerController.isCameraDeviceAvailable( UIImagePickerControllerCameraDevice.Front) {
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
presentViewController(imagePicker, animated: true, completion: nil)
}
}

Resources