swift camera overlayView - ios

I'm a newbie and I'm trying to make a simple app which lets you to take a photo and then see it with less opacity on the camera. Everything seems fine but the overlayView is zoomed in and i can't find the reason why. What should I do to make the overlayView on the camera to show the whole photo? here's my code:
import UIKit
class ViewController: UIViewController,
UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet var pickedImage: UIImageView!
var array = [UIImage]()
var temp = 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.
}
#IBAction func photoLibrary(_ sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary){
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary;
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true,completion: nil)
}
}
#IBAction func saveButton(_ sender: UIButton) {
let imageData = UIImageJPEGRepresentation( pickedImage.image!, 0.5)
let compressedJPEGImage = UIImage(data: imageData! )
UIImageWriteToSavedPhotosAlbum(compressedJPEGImage!, nil, nil, nil)
array.append(compressedJPEGImage!)
}
#IBAction func cameraButton(_ sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)
{
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
;
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true,completion: nil)
//-------------------------------------------------------------------
// let screenSize: CGRect = UIScreen.main.bounds
let overlayView = UIImageView(frame: imagePicker.cameraOverlayView!.frame)
let crosshairLabel = UIImageView()
if array.count >= 1{
var overlayViewInt = array.count - 1
crosshairLabel.image = array[overlayViewInt]
} else {
crosshairLabel.image = UIImage(named: "camera")
}
overlayView.bounds = UIScreen.main.bounds
crosshairLabel.alpha = 0.4
crosshairLabel.translatesAutoresizingMaskIntoConstraints = false
overlayView.addSubview(crosshairLabel)
crosshairLabel.centerXAnchor.constraint(equalTo: overlayView.centerXAnchor).isActive = true
crosshairLabel.centerYAnchor.constraint(equalTo: overlayView.centerYAnchor).isActive = true
// To avoid blocking the underneath default camera controls
overlayView.isUserInteractionEnabled = false
imagePicker.cameraOverlayView = overlayView
//-------------------------------------------------------------------
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject: AnyObject]!) {
pickedImage.image = image
self.dismiss(animated: true, completion: nil)
}
}

It is not very clear what view should actually show an image, but i assume it's the 'crosshairLabel'. You need to add width and height constraints to it:
crosshairLabel.heightAnchor.constraint(equalTo: overlayView.heightAnchor, multiplier: 1.0).isActive = true
crosshairLabel.widthAnchor.constraint(equalTo: overlayView.widthAnchor, multiplier: 1.0).isActive = true

Related

Checking which gestureRecognizer was pressed

I use gestureRecognizer with UIImageView. And I have two UIImageView. Me need upload image in both UIImageView. For opening UIImagePickerController() I use gestureRecognizer on both UIImageView.
So me need upload image in different UIImageView in func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
How I can upload image from library?
It's my code:
#IBOutlet weak var mainPhotoImageView: UIImageView!
#IBOutlet weak var addImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
configureGestureRecognizer()
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let chosenImage = info[UIImagePickerControllerEditedImage] as? UIImage
if chosenImage != nil {
logoImageView.image = chosenImage
dismiss(animated: true, completion: nil)
}
}
#objc func addLogoHall(_ sender: AnyObject) {
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
picker.sourceType = .photoLibrary
present(picker, animated: true, completion: nil)
}
#objc func addMainPhoto(_ sender: AnyObject) {
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
picker.sourceType = .photoLibrary
present(picker, animated: true, completion: nil)
}
func configureGestureRecognizer() {
let addHallLogo = UITapGestureRecognizer(target: self, action: #selector(AddPhotoAndLogoViewController.addLogoHall(_:)))
logoImageView.isUserInteractionEnabled = true
logoImageView.addGestureRecognizer(addHallLogo)
let addMainPhotoImage = UITapGestureRecognizer(target: self, action: #selector(AddPhotoAndLogoViewController.addMainPhoto(_:)))
mainPhotoImageView.isUserInteractionEnabled = true
mainPhotoImageView.addGestureRecognizer(addMainPhotoImage)
}
So in func imagePickerController me need detect which gestureRecognizer was used. Or maybe have alternative opportunity to do it?
You can add diffrent tag for pickers like this way:
#objc func addLogoHall(_ sender: AnyObject) {
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
picker.sourceType = .photoLibrary
picker.view.tag = 1
present(picker, animated: true, completion: nil)
}
#objc func addMainPhoto(_ sender: AnyObject) {
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
picker.view.tag = 2
picker.sourceType = .photoLibrary
present(picker, animated: true, completion: nil)
}
And then you can identify by this tag like this way:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let chosenImage = info[UIImagePickerControllerEditedImage] as? UIImage
if chosenImage != nil {
if(picker.view.tag == 1) {
logoImageView.image = chosenImage
} else {
mainImageView.image = chosenImage
}
dismiss(animated: true, completion: nil)
}
}
While Jogendar's answer is good, I see one pitfall. The if-else will grow as the number of image view instances increase in your view controller file. This might get messy and unnecessarily large quickly. Might I suggest an alternative solution:
class ImageSelectionController: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
weak var delegate: ImageSelectionControllerDelegate?
private weak var imageView: UIImageView?
private weak var presentingViewController: UIViewController?
// MARK: - Initialization
init(withImageView imageView: UIImageView?, andPresentingViewController viewController: UIViewController?) {
self.imageView = imageView
self.presentingViewController = viewController
}
// MARK: - Public
func presentImagePickerController(withConfigurationHandler handler: ((UIImagePickerController) -> Void)? = nil) -> Void {
let picker = UIImagePickerController()
picker.allowsEditing = true
picker.sourceType = .photoLibrary
handler?(picker)
picker.delegate = self
}
// MARK: - UIImagePickerController
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let chosenImage = info[UIImagePickerControllerEditedImage] as? UIImage
self.imageView?.image = chosenImage
self.delegate?.controller(self, didFinishPickingImage: chosenImage, forImageView: self.imageView)
picker.dismiss(animated: true, completion: nil)
}
}
protocol ImageSelectionControllerDelegate: AnyObject {
func controller(_ controller: ImageSelectionController, didFinishPickingImage image: UIImage?, forImageView: UIImageView?) -> Void
}
// ---- Your View Controller ---- //
#IBOutlet weak var logoImageView: UIImageView?
#IBOutlet weak var mainPhotoImageView: UIImageView?
override func viewDidLoad() {
super.viewDidLoad()
configureGestureRecognizer()
}
#objc func addLogoHall(_ sender: AnyObject) {
let controller = ImageSelectionController.init(withImageView: self.logoImageView, andPresentingViewController: self)
controller.delegate = self
controller.presentImagePickerController()
}
#objc func addMainPhoto(_ sender: AnyObject) {
let controller = ImageSelectionController.init(withImageView: self.logoImageView, andPresentingViewController: self)
controller.presentImagePickerController { (picker) in
picker.sourceType = .camera
}
}
// MARK: - ImageSelectionControllerDelegate
func controller(_ controller: ImageSelectionController, didFinishPickingImage image: UIImage?, forImageView: UIImageView?) {
print("image picked!")
}
Doing it this way, you could put all the UIImagePickerController related code in one place and you wouldn't need to have any if-else cases. Again, there is no right or wrong answer and someone might also find some other flaw in my solution. Its just a matter of how you want to structure your code.
Happy coding!

IOS Swift imagePickerController not call

class MyPage: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
var imagePicker = UIImagePickerController()
#IBOutlet weak var selectImage: UIButton!
override func viewDidLoad() {
imagePicker.sourceType = .photoLibrary
imagePicker.delegate = self
}
#IBAction func selectImage(_ sender: Any) {
print("a")
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
print("b")
imagePicker.allowsEditing = false
imagePicker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
self.present(imagePicker, animated: true,completion: nil)
}
#objc func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
self.dismiss(animated: true, completion: { () -> Void in
print("dismiss")
})
print(image)
selectImage.setImage(image, for: .normal)
}
}
If touch button print a and b and pop photoLibrary
but select Photo not call imagePickerController
do nothing after select photo
how to use imagepicker in swift 4?
Remove #objc from Delegate method and delegate method is this so replace your function with this :
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
and get image by using this :
guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {return}
import UIKit
class ViewController: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate {
let imageView = UIImageView()
let imagepicker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
self.imagepicker.delegate = self
imageView.frame = CGRect(x: 40, y: 150, width: 100, height: 100)
self.view.addSubview(imageView)
}
#IBAction func uploadImage(_ sender : UIButton) {
imagepicker.cameraDevice = .rear
imagepicker.cameraCaptureMode = .photo
imagepicker.sourceType = .photoLibrary
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage else {
return
}
imageView.image = image
print(image)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.dismiss(animated: true, completion: nil)
}
}

Add object on image Picker photo display (swift3)

The code below is code for just taking a standard photo using image picker. I am working on a app that masks a photo. So What I am trying to do is block out some of the image picker with a box. So the user knows that they should not put there face in the area with a box over it.
class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
var currentImageView: UIImageView?
var imagePicker: UIImagePickerController!
let maskView = UIImageView()
#IBOutlet var letImageVIEW: UIImageView!
#IBOutlet var finalImage: UIImageView!
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
self.dismiss(animated: true, completion: nil)
self.currentImageView?.image = image
}
#IBAction func takePhotoLeft(_ sender: Any) {
self.currentImageView = self.letImageVIEW
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
present(imagePicker, animated: true, completion: nil)
}
You can use UIImagePickerController property cameraOverlayView. Try this.
#IBAction func takePhotoLeft(_ sender: Any) {
self.currentImageView = self.letImageVIEW
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
let blockView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: 150))
blockView.backgroundColor = UIColor.red
imagePicker.cameraOverlayView = blockView
imagePicker.showsCameraControls = false
present(imagePicker, animated: true, completion: nil)
}

Change value of var outside the function

I declared the var imagePicked = 0 at the top of my class.
Now when I change the value of imagePicked inside an IBAction like here:
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet weak var titelbild: UIButton!
#IBOutlet weak var profilbild: UIButton!
let imagePicker = UIImagePickerController()
var imagePicked = 0
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
// imagePicker.allowsEditing = false
// imagePicker.sourceType = .PhotoLibrary
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func titelbildtapped(sender: AnyObject) {
// if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary){
imagePicked == 1
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary
self.presentViewController(imagePicker, animated: true, completion: nil)
// }
}
#IBAction func profilbildtapped(sender: AnyObject) {
imagePicked == 2
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary
self.presentViewController(imagePicker, animated: true, completion: nil)
print ("output")
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
if imagePicked == 1 {
titelbild.setImage(pickedImage, forState: UIControlState.Normal)
// titelbild.imageView?.image = pickedImage
} else if imagePicked == 2 {
profilbild.setImage(pickedImage, forState: .Normal) }
}
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
}
the value of imagePicked appears to be still 0 instead of 2. How can I change the value of it, so it is not only changed inside the function i changed it?
ok. the problem is in your titelbildtapped / profilbildtapped functions. there you have to assign the values 1 / 2 with a single = instead of double == which checks for equality.
so change imagePicked == 1 / imagePicked == 2 to imagePicked = 1 / imagePicked = 2 in those functions and it should work!

BaseMAP Gallery Popup for iPad

I am using ARCGIS library for GIS features, I wants to achieve basemap gallery like the one show in below picture.
how can I achieve above gallery, I tried to search sample code but did not get anything like shown in above picture.....
if possible if any one has done can give me the link to refer sample code...will be very helpful.
Here is a code for popup galary in IPad
import UIKit
class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIPopoverPresentationControllerDelegate {
var imagePicker = UIImagePickerController()
var popController:UIPopoverPresentationController!
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 btn_action(sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum) {
self.imagePicker.delegate = self
self.imagePicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
self.imagePicker.allowsEditing = true
imagePicker.modalPresentationStyle = UIModalPresentationStyle.Popover
self.presentViewController(self.imagePicker, animated: true, completion: nil)
popController = imagePicker.popoverPresentationController!
popController.permittedArrowDirections = UIPopoverArrowDirection.Up
popController.delegate = self
popController.sourceView = self.view
let contentSize : CGSize = CGSizeMake(500,400)
self.imagePicker.preferredContentSize = contentSize
popController.backgroundColor=UIColor.redColor()
popController.sourceRect = CGRectMake(sender.frame.origin.x + sender.bounds.width/2-5, sender.frame.origin.y+sender.bounds.size.height/2, 10, 10)
}
}
internal override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask
{
return UIInterfaceOrientationMask.Landscape
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
}
func imagePickerControllerDidCancel(picker: UIImagePickerController)
{
imagePicker.dismissViewControllerAnimated(true, completion: nil)
}
}
extension UIImagePickerController
{
public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask
{
return UIInterfaceOrientationMask.Landscape
}
}

Resources