How do I create a camera app that takes multiple photos swift - ios

I am trying to create an app that takes more than a single photo. What I want is every time​ I take a photo I want the camera opens right after the first photo.
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet var myImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func takePhoto(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerController.SourceType.camera
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
myImage.contentMode = .scaleToFill
myImage.image = pickedImage
}
picker.dismiss(animated: true, completion: nil)
}
}

Related

Can you have two different camera buttons for two different image views on one view controller? (Swift 3)

Here is my code below: Basically I am trying to give the user an option to choose the camera button1 or choose from photo library button1. Then depending on the button they press it adds to imageview1. Then on the same view controller it is duplicated on the bottom half of the display with a camera button2 and a photo library button 2 that will go to the imageview2.
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextFieldDelegate {
#IBOutlet weak var ImageView1: UIImageView!
#IBOutlet weak var ImageView2: UIImageView!
#IBOutlet weak var textField1: UITextField!
#IBOutlet weak var textField2: UITextField!
var imagePicker = UIImagePickerController()
var cameraImagePicker = UIImagePickerController()
var imagePicked = 0
var cameraPicked = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = false
cameraImagePicker.delegate = self
cameraImagePicker.sourceType = .camera
cameraImagePicker.allowsEditing = false
textField1.delegate = self
textField2.delegate = self
}
#IBAction func uploadImageBtnClick1(_ sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary){
print("Working")
imagePicked = sender.tag
self.present(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
if imagePicked == 0{
ImageView1.image = pickedImage
} else if imagePicked == 1{
ImageView2.image = pickedImage
} else {
print("Something went wrong")
}
if cameraPicked == 2 {
ImageView1.image = pickedImage
} else if cameraPicked == 3 {
ImageView2.image = pickedImage
}
} else {
print("Something went wrong")
}
dismiss(animated: true, completion: nil)
}
#IBAction func CameraButtonTapped(_ sender: UIButton) {
cameraPicked = sender.tag
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
print("working")
self.present(cameraImagePicker, animated: true, completion: nil)
}
}
}
The problem is your use of two different variables to track which image view should be updated. Either reset both variables after updating an image view or use only one variable.
And there is no need for two UIImagePickerController instances.
Lastly, update the tags of all 4 buttons. For the two buttons that affect ImageView1, set their tag to 1. For the two buttons that affect ImageView2, set their tag to 2. Never use a tag of 0 since all views have a default tag of 0.
BTW - name variables and functions with a leading lowercase letter. Only class and struct names should start with uppercase.
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextFieldDelegate {
#IBOutlet weak var imageView1: UIImageView!
#IBOutlet weak var imageView2: UIImageView!
#IBOutlet weak var textField1: UITextField!
#IBOutlet weak var textField2: UITextField!
var imagePicked = 0
override func viewDidLoad() {
super.viewDidLoad()
textField1.delegate = self
textField2.delegate = self
}
#IBAction func uploadImageBtnClick1(_ sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary){
print("Working")
imagePicked = sender.tag
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
}
#IBAction func cameraButtonTapped(_ sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
print("working")
imagePicked = sender.tag
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
if imagePicked == 1 {
imageView1.image = pickedImage
} else if imagePicked == 2 {
imageView2.image = pickedImage
} else {
print("Something went wrong")
}
} else {
print("Something went wrong")
}
dismiss(animated: true, completion: nil)
}
}

How to add crop functionality in camera taker app

I have implemented picking image from gallery or take photo and i am displaying in in uiimage view in screen. But i want to implement crop functionality.
Measn that, after i take photo from phone then i need to crop and then i need to display in my screen. How to do that ??
Here my code :
var imagePicker: UIImagePickerController!
#IBOutlet weak var imagevView: UIImageView!
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 ChooseImage(_ sender: AnyObject) {
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
imagePicker.dismiss(animated: true, completion: nil)
imagevView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}
Thanks
You can, to some extend, use the imagePicker.allowsEditing = true setting. If you want more advanced cropping something like what #Kamlesh suggest might be what you are looking for.

How to add multiple image view and each image view can do image picker?

enter image description hereI'm making dating app.
so users need to update their images in profile.
In profile section,
there are small 3 image views.
and if they want to add image to each image views,
they should update each image view by UIimagepicker.
but I know I can only use only one function in UIImagepikerControllerDelegate protocol.
how can I do this?
below are my failed code
import UIKit
class RegisterPicture : UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBAction func pick1(sender: AnyObject) {
let picker1 = UIImagePickerController()
picker1.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
picker1.allowsEditing = true
picker1.delegate = self
self.presentViewController(picker1, animated: false, completion: nil)
}
#IBAction func pick2(sender: AnyObject) {
let picker2 = UIImagePickerController()
picker2.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
picker2.allowsEditing = true
picker2.delegate = self
self.presentViewController(picker2, animated: false, completion: nil)
}
#IBAction func pick3(sender: AnyObject) {
let picker = UIImagePickerController()
picker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
picker.allowsEditing = true
picker.delegate = self
self.presentViewController(picker, animated: false, completion: nil)
}
#IBOutlet var picture1: UIImageView!
#IBOutlet var picture2: UIImageView!
#IBOutlet var picture3: UIImageView!
func imagePickerController(picker1: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
picker1.dismissViewControllerAnimated(false, completion : nil)
}
func imagePickerController(picker2: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
picker2.dismissViewControllerAnimated(false, completion : nil)
self.picture2.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}
func imagePickerController(picker3: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
picker3.dismissViewControllerAnimated(false, completion : nil)
self.picture3.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
picker.dismissViewControllerAnimated(false, completion:nil)
}
func imagePickerControllerDidCancel(picker2: UIImagePickerController) {
picker2.dismissViewControllerAnimated(false, completion:nil)
}
func imagePickerControllerDidCancel(picker3: UIImagePickerController) {
picker3.dismissViewControllerAnimated(false, completion:nil)
}
Not sure if I understood what you need but what I suggest is that those UIImageViews could be in a collection view.
By doing that, the only delegate for the image picker would be the view controller that holds the collection view of the images and you would just update the selected cell with the picked image
You give the tag to each imageView when you are creating imageView and
inside this method
-(void)imagePickerController:(UIImagePickerController )picker didFinishPickingMediaWithInfo:(NSDictionary )info
with the help of if else condition you can use each imageView separately.
You can create this as a new project to not deal with assigned actions - or you can clear the actions and outlets in your view and recreate according to the new code.
Create 3 UIImageViews, picture1, picture2, picture3. Link the outlets in the code.
Create a UIToolbar with 3 UIBarButtonItems. Set bar button item tags to be 1,2,3 corresponding to the picture1, picture2, picture3 image views.
Connect outlets for image views (picture1,2,3)
Connect picker action to ALL THREE bar button items. Those will be using the same action, no need to repeat code, difference will be detected by the sender button's tag.
Follow the code, it will explain itself. It just decides which UIImageView will be the "active" one during the image pick according to the clicked bar button then delegate will use that image view.
Let us know if doesn't make sense.
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBAction func picker(sender: UIBarButtonItem) {
imagePicker.allowsEditing = false
imagePicker.sourceType = .SavedPhotosAlbum
switch sender.tag {
case 2:
self.imageView = picture2
case 3:
self.imageView = picture3
default:
self.imageView = picture1
}
presentViewController(imagePicker, animated: true, completion: nil)
}
#IBOutlet weak var picture1: UIImageView!
#IBOutlet weak var picture2: UIImageView!
#IBOutlet weak var picture3: UIImageView!
var imageView: UIImageView!
let imagePicker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
self.imagePicker.delegate = self
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
self.imageView.contentMode = .ScaleAspectFit
self.imageView.image = pickedImage
}
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
}
EDIT: Here is the sample project I just uploaded:
https://github.com/smozgur/Multiple-Image-Picker

UIView setImage:]: unrecognized selector sent to instance

import UIKit
import MobileCoreServices
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate{
let imagePicker = UIImagePickerController()
#IBOutlet var ImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
imagePicker.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: Actions
#IBAction func Select(sender: AnyObject) {
//imagePicker.sourceType =
// UIImagePickerControllerSourceType.PhotoLibrary
//imagePicker.mediaTypes = [kUTTypeImage as String]
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
ImageView.contentMode = .ScaleAspectFit
ImageView.image = image
}
self.dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
self.dismissViewControllerAnimated(true, completion: nil)
}
}
I stumped on why the UIImage picked by the PickerController is not set to UIImageView. Can any one please provide insight into what I am missing here?
You have #IBOutlet var ImageView: UIImageView! in your code, but you are getting the error "UIView setImage:]: unrecognized selector". This means that you configured your view in Interface Builder as a UIView, rather than a UIImageView. You need to set up a UIImageView to be able to set the image.
Just for future reference the other reason this might happen is because you've accidentally connected the wrong UI element to your UIImageView property.
For example I dragged a NSConstraint to the UIImageView, and got the same error.

Implementing UIImagePickerController in Swift

The application runs just fine and allows me to select a photo. However, when I go to select a different photo the pickAnImage button won't work. What is the best way to allow the user to select a different image to view? Any help is appreciated.
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate,UINavigationControllerDelegate {
#IBOutlet weak var imageView: UIImageView!
let imagePicker = UIImagePickerController()
#IBAction func pickAnImage(sender: AnyObject) {
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary
self.presentViewController(imagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
imageView.contentMode = .ScaleAspectFit
self.imageView.image = pickedImage
}
dismissViewControllerAnimated(true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
imagePicker.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
try this code :
import MobileCoreServices
class SecondViewController: UIViewController,UINavigationControllerDelegate, UIImagePickerControllerDelegate {
#IBOutlet var img:UIImageView!=nil
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
#IBAction func buttonTapped(AnyObject)
{
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary){
println("Button capture")
var imag = UIImagePickerController()
imag.delegate = self
imag.sourceType = UIImagePickerControllerSourceType.PhotoLibrary;
//imag.mediaTypes = [kUTTypeImage];
imag.allowsEditing = false
self.presentViewController(imag, animated: true, completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!) {
let selectedImage : UIImage = image
//var tempImage:UIImage = editingInfo[UIImagePickerControllerOriginalImage] as UIImage
img.image=selectedImage
self.dismissViewControllerAnimated(true, completion: nil)
}
}

Resources