imagepicker doesnt show the image when using cameraoverlay - ios

I wanted to customize the camera scene so i used custom overlay. It is hiding the default camera buttons and showing other buttons. When i take the picture, I want to see the static image but I see only the camera again.I tried putting a image view in the customView but the camera still runs in the background.
func cameraProperty()
{
DispatchQueue.main.async(execute: {
//let imagePicker = UIImagePickerController()
self.picker.navigationBar.isTranslucent = false
self.picker.navigationBar.barTintColor = .blue // Background color
self.picker.navigationBar.tintColor = .white // Cancel button ~ any UITabBarButton items
self.picker.navigationBar.titleTextAttributes = [
NSForegroundColorAttributeName : UIColor.white
] //
self.picker.sourceType = UIImagePickerControllerSourceType.camera
self.picker.cameraDevice = .front
self.picker.delegate = self
self.picker.showsCameraControls = false
self.picker.modalPresentationStyle = .fullScreen
//customView stuff
let customViewController = CustomOverlayViewController(
nibName:"CustomOverlayViewController",
bundle: nil
)
let customView:CustomOverlayView = customViewController.view as! CustomOverlayView
customView.frame = self.picker.view.frame
customView.delegate = self
customView.skipBtn.setTitle("SKIP", for: .normal)
customView.createSongBtn.isHidden = true
customView.takePhotoBtn.isHidden = false
customView.curImg.isHidden = true
//CGSize screenSize = [[UIScreen mainScreen] bounds].size;
let screenSize:CGSize = UIScreen.main.bounds.size
let ratio:CGFloat = 4.0 / 3.0
let cameraHeight:CGFloat = screenSize.width * ratio
let scale:CGFloat = screenSize.height / cameraHeight
//need to test it for different phone sizes
self.picker.cameraViewTransform = CGAffineTransform(translationX: 0, y: (screenSize.height - cameraHeight) / 2.0)
self.picker.cameraViewTransform = self.picker.cameraViewTransform.scaledBy(x: scale, y: scale)
self.picker.cameraOverlayView = customView
self.picker.allowsEditing = false
})
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController){
picker.dismiss(animated: true, completion: nil)
}
//MARK: Custom View Delegates
func didCancel(overlayView:CustomOverlayView) {
DispatchQueue.main.async(execute: {
if overlayView.skipBtn.titleLabel?.text == "SKIP"
{
self.picker.dismiss(animated: true,completion: nil)
print("dismissed!!")
}
else
{
overlayView.skipBtn.setTitle("SKIP", for: .normal)
overlayView.createSongBtn.isHidden = true
// overlayView.curImg.isHidden = true
overlayView.takePhotoBtn.isHidden = false
print("Reset camera")
}
})
}
func didShoot(overlayView:CustomOverlayView) {
DispatchQueue.main.async(execute: {
self.picker.takePicture()
overlayView.skipBtn.setTitle("Try Again", for: .normal)
// overlayView.sendSubview(toBack: overlayView.curImg)
overlayView.createSongBtn.isHidden = false
overlayView.takePhotoBtn.isHidden = true
})
print("Shot Photo")
}
How can I show the view with image(that is also custom view) and something like "Use photo" option.

Related

How to present popover from tab bar item?

I want to present a ViewController as popover in Swift 4, but it presents the Viewcontroller normally, this is the code:
class CustomTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let favViewController = TrialViewController()
let exhibtionViewController = TrialViewController()
let menuViewController = ttttViewController()
let notificationViewController = TrialViewController()
let profileViewController = ttttViewController()
favViewController.tabBarItem.title = "first"
exhibtionViewController.tabBarItem.title = "second"
menuViewController.tabBarItem.title = "third"
notificationViewController.tabBarItem.title = "forth"
profileViewController.tabBarItem.title = "fifth"
favViewController.tabBarItem.image = UIImage(named:"home25")
exhibtionViewController.tabBarItem.image = UIImage(named: "bag25")
menuViewController.tabBarItem.image = UIImage(named: "main_add_25")
notificationViewController.tabBarItem.image = UIImage(named: "notification25")
profileViewController.tabBarItem.image = UIImage(named: "man_man25")
let tabBarItemWidth = Int(self.tabBar.frame.size.width) / (self.tabBar.items?.count)!
let x = tabBarItemWidth * 3;
let newRect = CGRect(x: x, y: 0, width: tabBarItemWidth, height: Int(self.tabBar.frame.size.height))
print(newRect)
menuViewController.modalPresentationStyle = .popover
menuViewController.view.frame = newRect
menuViewController.preferredContentSize = CGSize(width: 150,height: 150)
if let popoverMenuViewController = menuViewController.popoverPresentationController {
popoverMenuViewController.permittedArrowDirections = .down
popoverMenuViewController.delegate = menuViewController as? UIPopoverPresentationControllerDelegate
popoverMenuViewController.sourceRect = newRect
popoverMenuViewController.sourceView = self.tabBar
present(menuViewController, animated: true, completion: nil)
}
viewControllers = [favViewController, exhibtionViewController, menuViewController, notificationViewController, profileViewController]
}
}
what is the problem with my code?
override func viewDidAppear(_ animated: Bool) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "bbb") as! ttttViewController
vc.modalPresentationStyle = .popover //presentation style
vc.preferredContentSize = CGSize(width: 150,height: 150)
vc.popoverPresentationController?.delegate = self as! UIPopoverPresentationControllerDelegate
vc.popoverPresentationController?.sourceView = view
vc.popoverPresentationController?.sourceRect = self.tabBar.frame
self.present(vc, animated: true, completion: nil)
}
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
demo is here popover

Image Selected From Image Picker Not Displaying

I'm kind of lost. My Image Picker is working but the image is not displaying in my Image View. I have looked over my code and various solutions and it still is not working. I have set the delegate to self and double checked my methods and it is still not showing the image where it is supposed to.
import UIKit
class AlterProfileViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
view?.backgroundColor = UIColor.white
navigationItem.title = "Profile Settings"
view.addSubview(selectProfileImage)
///Constraints for all views will go here
_ = selectProfileImage.anchor(view.centerYAnchor, left: view.leftAnchor, bottom: nil, right: nil, topConstant: -275, leftConstant: 135, bottomConstant: 0, rightConstant: 0, widthConstant: 100, heightConstant: 100)
// selectProfileImage.layer.cornerRadius = selectProfileImage.frame.size.width/2
///////////////////////////////////////////////
// Do any additional setup after loading the view.
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
selectProfileImage.layer.cornerRadius = selectProfileImage.frame.size.width / 2;
selectProfileImage.layer.masksToBounds = true
}
//Where all buttons and labels will be added
//will just be a nice looking image view to be next to the profile settings button
lazy var selectProfileImage: UIImageView = {
let selectPicture = UIImageView()
// self.selectProfileImage.layer.cornerRadius = self.selectProfileImage.frame.size.width / 2;
selectPicture.image = UIImage(named: "Paris")
// selectPicture.layer.cornerRadius = selectPicture.frame.size.width / 2;
selectPicture.clipsToBounds = true
selectPicture.translatesAutoresizingMaskIntoConstraints = false
selectPicture.layer.cornerRadius = selectPicture.frame.size.width/2
selectPicture.contentMode = .scaleAspectFill
selectPicture.isUserInteractionEnabled = true
selectPicture.layer.shouldRasterize = true
// will allow you to add a target to an image click
selectPicture.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleSelectProfileImageView)))
selectPicture.layer.masksToBounds = true
return selectPicture
}()
func handleSelectProfileImageView() {
print("123")
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
present(picker, animated: true, completion: nil)
}
// will dispaly info of image selected
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
print("info")
var selectedImageFromPicker: UIImage?
if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage{
print((editedImage as AnyObject).size)
selectedImageFromPicker = editedImage
}else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage{
print((originalImage as AnyObject).size)
selectedImageFromPicker = originalImage
}
if let selectedImage = selectedImageFromPicker {
selectProfileImage.image = selectedImage
}
dismiss(animated: true, completion: nil)
}
// will handle the picker being closed/canceled
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
print("picker canceled")
dismiss(animated: true, completion: nil)
}
///////////////////////////////////////////////////////////////////////////////////
}

Scrollview only zooms to upper left corner

I've created a scrollview programmatically and added an image as it's subview but when I try to zoom into the image it only zooms into the upper corner and it doesn't let me scroll around to see different parts of the image.
Here's the code I used to create the scrollview:
let scrollView = UIScrollView()
var image: NSData!
override func viewDidLoad() {
super.viewDidLoad()
scrollView.minimumZoomScale = 1.0
scrollView.maximumZoomScale = 3.0
scrollView.zoomScale = 1.0
scrollView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
scrollView.delegate = self
scrollView.isPagingEnabled = false
scrollView.contentSize = CGSize(width: self.view.frame.width, height: self.view.frame.height)
view.addSubview(scrollView)
scrollView.addSubview(imageView)
imageView.image = UIImage(data: image as Data)
imageView.contentMode = .scaleAspectFit
imageView.isUserInteractionEnabled = true
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(share))
longPress.minimumPressDuration = 0.5
longPress.numberOfTapsRequired = 0
imageView.addGestureRecognizer(longPress)
let doubleTap = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap(recognizer:)))
doubleTap.numberOfTapsRequired = 2
scrollView.addGestureRecognizer(doubleTap)
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imageView
}
func share(sender: UILongPressGestureRecognizer) {
if sender.state == .ended {
if UIImage(data: image as Data) != nil {
let Item = UIImage(data: self.image! as Data)
let activity = UIActivityViewController(activityItems: [Item!], applicationActivities: nil)
activity.excludedActivityTypes = [UIActivityType.print, UIActivityType.addToReadingList, UIActivityType.openInIBooks]
self.present(activity, animated: true, completion: nil)
}
}
}
func handleDoubleTap(recognizer: UITapGestureRecognizer) {
if scrollView.zoomScale > scrollView.minimumZoomScale {
scrollView.setZoomScale(scrollView.minimumZoomScale, animated: true)
} else {
scrollView.setZoomScale(scrollView.maximumZoomScale, animated: true)
}
}
Thank you very much for any kind of help
The issue is in your code when you set the zoomscale of your scrollView. You don't specify where it should zoom to. So naturally, it zooms and stays in the top left corner.
You need to get the location of the touch in your handleDoubleTap(recognizer: UITapGestureRecognizer), calculate the rect for zoom, and call scrollRectToVisible on your scrollview.

zoom image with scrollview swift ios

I have a problem with zooming an image with scroll view.
If I put the scroll view on the left with width 125 and height 375, it does not zoom the image.
It only works if I put the scroll view on the center of the view.
Screenshot:
Why is the scroll view not zooming when it's on the left side?
Here is my code
var imgview:UIImageView!
var imagepicked:UIImage!
var minZoomScale:CGFloat!
let picker = UIImagePickerController()
#IBOutlet weak var scrollViewSquare: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
scrollViewSquare.delegate = self
picker.allowsEditing = false
picker.sourceType = .PhotoLibrary
}
func imagePickerController(
picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : AnyObject])
{
imagepicked = (info[UIImagePickerControllerOriginalImage] as? UIImage)!
ImageViewInit()
dismissViewControllerAnimated(false, completion: nil)
}
#IBAction func Pick(sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
self.presentViewController(picker, animated: true, completion: nil)
}
}
func ImageViewInit(){
imgview = UIImageView()
imgview.frame = CGRectMake(0, 0, imagepicked.size.width, imagepicked.size.height)
imgview.image = imagepicked
imgview.contentMode = .ScaleAspectFit
imgview.backgroundColor = UIColor.greenColor()
scrollViewSquare.maximumZoomScale = 4;
scrollViewSquare.minimumZoomScale = 0.02;
scrollViewSquare.bounces = true;
scrollViewSquare.bouncesZoom = true;
scrollViewSquare.contentMode = .ScaleAspectFit
scrollViewSquare.contentSize = imagepicked.size
scrollViewSquare.autoresizingMask = UIViewAutoresizing.FlexibleWidth
scrollViewSquare.addSubview(imgview)
setZoomScale()
}
func setZoomScale(){
let imageViewSize = imgview.bounds.size
let scrollViewSize = scrollViewSquare.bounds.size
let widthScale = scrollViewSize.width / imageViewSize.width
let heightScale = scrollViewSize.height / imageViewSize.height
minZoomScale = max(widthScale, heightScale)
scrollViewSquare.minimumZoomScale = minZoomScale
scrollViewSquare.zoomScale = minZoomScale
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return imgview
}
Hello #Bensalem Ilyes,
I've tried your code and zooming does not work on simulator because the centre of pinch gesture is in the midle so pinch on scrollview is translated as simple pan gesture. I tried your code on real device and zooming works fine.
For repositioning pinch gesture on simulator:
alt+shift key

WYPopoverController on UITabBarController falls

I create button on UITabBarCOntroller and try to call modal view over all screens in application. I try to use WYPopoverController as a solution
My UITabBarController class is
class PlanetTabBarController: UITabBarController, WYPopoverControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.tabBar.tintColor = kTintColor
var items = self.tabBar.items as! [UITabBarItem]
let centredTabBar:UITabBarItem = items[2]
self.tabBar.layer.borderWidth = 0.50
centredTabBar.image = UIImage()
self.tabBar.layer.borderColor = UIColor.clearColor().CGColor
self.tabBar.layer.borderWidth = 0
self.tabBar.shadowImage = UIImage()
// self.tabBar.backgroundImage = ktabBarImage
self.tabBar.backgroundImage?.imageWithAlignmentRectInsets
print( UIDevice.currentDevice().modelName)
if( UIDevice.currentDevice().modelName != "iPhone 6" && UIDevice.currentDevice().modelName != "iPhone 6 Plus"){
self.tabBar.backgroundImage = ktabBarImage
}
if( UIDevice.currentDevice().modelName == "iPad 2" ){
self.tabBar.backgroundImage = ktabBarImage
}
if let font = UIFont(name: "Avenir-Black", size: 10) {
let appearance = UITabBarItem.appearance()
let attributes = [NSFontAttributeName:font]
appearance.setTitleTextAttributes(attributes, forState: UIControlState.Normal)
}
let buttonImage:UIImage = UIImage(named: "tab_planet")!
var button: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height)
button.setBackgroundImage(buttonImage, forState: UIControlState.Normal)
var heightDifference:CGFloat = buttonImage.size.height - self.tabBar.frame.size.height
if (heightDifference < 0){
button.center = self.tabBar.center
button.center.y = self.tabBar.center.y - 5
}else{
var center:CGPoint = self.tabBar.center
center.y = center.y - heightDifference/2.0 - 5
button.center = center
}
button.addTarget(self, action: "addPlanet:", forControlEvents: .TouchUpInside)
self.view.addSubview(button)
}
func addPlanet(sender: UIButton!) {
self.showAddPlan()
}
func showAddPlan(){
var contentViewController = UIStoryboard(name: "AddPlan", bundle: nil).instantiateViewControllerWithIdentifier("AddPlanController") as? AddPlanController
var popoverController = WYPopoverController(contentViewController: contentViewController)
popoverController.delegate = self
popoverController.presentPopoverFromRect(CGRectZero, inView: nil, permittedArrowDirections: WYPopoverArrowDirection.None, animated: false) { () -> Void in
print ("done")
}
}
func popoverControllerShouldDismissPopover(popoverController: WYPopoverController!) -> Bool {
return false
}
}
But when I try to call my AddPlanController it crashed with error
**An instance 0x7fe4a48849d0 of class Planet.AddPlanController was deallocated while key value observers were still registered with it. Current observation info: (
Context: 0x0, Property: 0x7fe4a4915e00>
**
Please give me an advice how to call something like modal view controller from button on UITabBar, that will be over all screens, with shadow bg. =(
You have to keep reference to popoverController until it's visible, but now it deallocated when you return from showAddPlan method.
Move definition of popoverController from your function and it will work

Resources