UIImagePickerController - Choose saved images in the camera view - ios

I am able to start the UIImagePickerController to make a picture:
func selectCamera(){
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera;
imagePicker.allowsEditing = true
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}
and with this function to choose from the saved pictures:
func selectPicture() {
let picker = UIImagePickerController()
picker.allowsEditing = true
picker.delegate = self
presentViewController(picker, animated: true, completion: nil)
}
I can either choose a image from my image gallery on the phone or take a picture.
But i want to choose one of the saved image gallery pics inside the camera view, how it is in the standard camera application. Is this possible?

If you want to have Gallery Pics inside camera view, you got to create a custom cameraOverlay view, which has a button to then open the saved pictures.
To make it faster, you can use a library like --> https://github.com/GabrielAlva/Cool-iOS-Camera
Or
You can also have a action sheet, asking user to select which option he wants.
Check this -> How to allow the user to pick a photo from his camera roll or photo library?
& this --> https://github.com/chroman/CRMediaPickerController

I am using this solution, without additional libraries.
First of all don't forget to delegate, UIImagePickerControllerDelegate, and navigation delegate. Those thigns needed only for imagePickerController.
Update, check function saveImageViewToAlbum, be sure that your imageView containt image, otherwise you probably will catch exception. Adds a photo to the saved photos album. The optional completionSelector should have the form:
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
Just created this project, and everything works on swift, iOS 9.3
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet weak var choosenImageView: 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 openAlbum(sender: UIButton) {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self .presentViewController(imagePickerController, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
choosenImageView.image = image
self .dismissViewControllerAnimated(true, completion: nil)
}
#IBAction func saveImageViewToAlbum(sender: UIButton) {
UIImageWriteToSavedPhotosAlbum(choosenImageView.image!, nil, nil, nil)
}
}

Related

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

How do I close the camera interface after a certain time on xcode in swift2 and open a different view controller?

So I have a button in the initial control view which takes the user to the camera interface. The user goes back to the initial view controller after taking a photo but how do I make the app close the camera and return to the initial page or a different page after a certain time period if the user doesn't take a picture.
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet weak var CameraButton: UIButton!
#IBOutlet weak var photoLibrary: UIButton!
#IBOutlet weak var imageView: UIImageView!
#IBAction func cameraAction(sender: UIButton) {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .Camera
presentViewController(picker, animated: true, completion: nil)
}
#IBAction func photoLibraryAction(sender: AnyObject) {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .PhotoLibrary
presentViewController(picker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
imageView.image = info [UIImagePickerControllerOriginalImage] as? UIImage;
dismissViewControllerAnimated(true, completion: nil)
}
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.
}
}
Create a NSTimer that gets started when you present the image picker. Set your desired waiting time.
Then, in timer fire method dismiss the image picker.
If user dismisses the image picker before the timer fires, invalidate the timer in didFinishPickingMediaWithInfo

Camera Button Not Working

I have an app that allows users to take selfies and upload it to Facebook, but the selfieTapped button I created is not working. It brings up a black screen. It does not let the user pick a photo from their library nor does it allow the user to take a picture.
import UIKit
import Social
class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
#IBOutlet weak var myImageView: UIImageView!
#IBAction func selfieTapped(sender: AnyObject) {
//this method will be called when the user has taken a photo or has selected a photo from the photo library
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!) {
//takes the image parameter and displays it inside myImageView
myImageView.image = image
//hides the imagePicker and animates the transition
self.dismissViewControllerAnimated(true, completion: nil)
}
//creates a new UIImagePickerController and sets it to the imagePicker variable
let imagePicker = UIImagePickerController()
//self the imagePicker's delegate property to the current view controller
imagePicker.delegate = self
//sets the imagePicker's sourceType property to .Camera
if UIImagePickerController.isSourceTypeAvailable(.Camera) {
//checking if a front camera is available
imagePicker.sourceType = .Camera
//if so, set it to the front camera
if (UIImagePickerController.isCameraDeviceAvailable(.Front)) {
//setting it to the front camera
imagePicker.cameraDevice = .Front
} else {
//setting it to the rear camera if the front is not available
imagePicker.cameraDevice = .Rear
}
} else {
//if the camera is not available, the photo library will be shown
imagePicker.sourceType = .PhotoLibrary
}
//presents the imagePicker modally, sliding it up from the bottom of the screen and it animates the transition
self.presentViewController(imagePicker, animated: true, completion: nil)
}
#IBAction func shareTapped(sender: AnyObject) {
//sets the serviceType property to Facebook
let facebookSocial = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
//myImageView image is added to the Facebook post
facebookSocial.addImage(myImageView.image)
//it is displayed and uses animation
self.presentViewController(facebookSocial, animated: true, completion: nil)
}
}
Please check if you have closed Camera & Photos permission for your app in Setting.
This may not be the cause of the trouble, but it certainly will be trouble later: you've put one function inside of another:
#IBAction func selfieTapped(sender: AnyObject) {
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!) {
Don't do that. They both need to be at the same level, as methods of your class. Otherwise, didFinishPickingImage can never be called.

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