Implementing UIImagePickerController in Swift - ios

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)
}
}

Related

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

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)
}
}

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

After selecting image via UIImagePickerController none of the IBAction work in toolbar

I'm working with Swift 2 in xCode 7. Building a single view app. What I'm trying to do is allow the user to access their photo library, select an image, and then record some audio.
I setup an UIImageView view for where the selected photo will go and then a UIToolbar with three UIBarButtonItems: Image , Record, Play. Clicking on 'Image' will bring up the user's photo library to pick a photo from.
Here is my code:
import UIKit
import AVFoundation
class ViewController: UIViewController, AVAudioPlayerDelegate, AVAudioRecorderDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet var imageView: UIImageView!
#IBOutlet var imgSlctBtn: UIBarButtonItem!
#IBOutlet var recordBtn: UIBarButtonItem!
#IBOutlet var playBtn: UIBarButtonItem!
#IBOutlet var toolbar: UIToolbar!
var soundRecorder : AVAudioRecorder!
var soundPlayer : AVAudioPlayer!
var fileName = "micFlagRecording"
var imagePicker = UIImagePickerController()
var newFileName = ""
let userDefaults = NSUserDefaults.standardUserDefaults()
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
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 getCacheDirectory() -> String {
. . .
}
func saveFileReference() {
. . .
}
func newFileURL() -> NSURL {
. . .
}
func getFileURL() -> NSURL {
. . .
}
func setupRecorder() {
. . .
}
func preparePlayer() {
. . .
}
#IBAction func record(sender: UIBarButtonItem) {
print("record started")
if sender.title == "Record" {
soundRecorder.record()
recordBtn.title = "Stop"
} else {
soundRecorder.stop()
recordBtn.title = "Record"
playBtn.enabled = true
}
}
#IBAction func play(sender: UIBarButtonItem) {
if sender.title == "Play" {
recordBtn.enabled = false
sender.title = "Stop"
preparePlayer()
soundPlayer.play()
} else {
soundPlayer.stop()
sender.title = "Play"
recordBtn.enabled = true
}
}
#IBAction func initImgSlct(sender: UIBarButtonItem) {
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary
presentViewController(imagePicker, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
playBtn.enabled = false
imagePicker.delegate = self
setupRecorder()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
What happens when I try out this code is recording and play back work just fine. However, after I select an image from the photo library by clicking "Image" (which calls the IBAction func initImgSlct) the UIBarButtonItems are still clicking but none of the IBActions are triggered.
I was thinking the issue has to do with my dismissViewController on ImagePickerController is not returning delegation back to my ViewController? I'm new to Swift so that could be way off.
Thanks for your patience and help!

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.

Resources