2

I'm trying to make a custom camera view, basically. I am stuck changing the size of the image view inside of the UIImagePicker. By the image view, I mean the window that shows the view of the camera. I hope it will be more clear from the picture. It is the view inside of that red square (I drawed that red square manually to show you, do not think it is from code).

sample camera view

Whatever I tried so far could not help me to change the size of that red rectangular area. Below is what I have so far:

@IBAction func takePhoto(sender: UIButton) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){
        imagePicker =  UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = .Camera
        imagePicker.allowsEditing = false
        imagePicker.showsCameraControls = false

        //Create view
        let customViewController = CustomCameraViewController(
            nibName:"CustomCameraViewController",
            bundle: nil
        )
        let customView:CustomCameraView = customViewController.view as! CustomCameraView

        //Assignments
        customView.frame = self.imagePicker.view.frame
        customView.delegate = self
        self.imagePicker.cameraOverlayView?.frame.size = customView.image.frame.size

        presentViewController(imagePicker,
            animated: true,
            completion: {
                self.imagePicker.cameraOverlayView = customView
            }
        )
    }else{
        //Alert
        let alert = UIAlertController(title: "Üzgünüz, kameranızı açamadık!", message: "Telefonunuz kamerayı desteklemiyor veya açmamıza izin verilmemiş. İzin vermek isterseniz ayarlardan izni açabilirsiniz.", preferredStyle: UIAlertControllerStyle.Alert)

        //Creating actions
        let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default){
            UIAlertAction in
            NSLog("OK is Pressed. Proceeding...")
        }

        //Adding actions
        alert.addAction(okAction)

        //Display alert
        self.presentViewController(alert, animated: true, completion: nil)
    }
}

I am trying to accomplish whole custom view thing from a xib file and it is related to CustomCameraView class. I set up two delegate methods to the CustomCameraView in able to use those but these are not necessary for this question so I am not gonna post the code but still I think you might need to know. What is related to the question is the xib file, so please find it below:

CustomCameraViewController.xib file

The buttons are showing up at where they supposed to be however I cant make use of that imageView. I created an image view here so that I can take its frame.size and set to the picker's frame.size (you can see this in my code listing). And of coruse the reference to that image view is in CustomCameraView class, so I am reaching from that class as you might notice also.

I have been trying for a whole day honestly yet I could not find a solution for that either or myself on net. I have read AVFoundation also but seems like there is not a clear explanation of how to set the frame size of that view in AVFoundation documentations as in UIImagePicker so I just decided to ask you guys as I am so lost in the project and cannot think another way right now.

I would be really appreciate if you can help me. Let me know if you need further information.

Thanks!

Kutay Demireren
  • 640
  • 1
  • 10
  • 25
  • You should set the cameraViewTransform of imagePicker to get it in the position/scale you desire. The camera has a native aspect ratio for different modes (photo/video) and is thus resistant to just being set to some arbitrary value. – beyowulf Feb 29 '16 at 18:15
  • Thanks for your comment. Can you give me a small example of usage of cameraViewTransform? I would be appreciated, and if you do please do it as answer so that it can be easier to see. – Kutay Demireren Feb 29 '16 at 18:52

1 Answers1

5

Not sure how you want to change the frame of the cameraView, but you can say something like the following to move the view 100 points down the screen:

 if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){
            imagePicker =  UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = .Camera
            imagePicker.allowsEditing = false
            imagePicker.showsCameraControls = false

            //Create view
            let customViewController = CustomCameraViewController(
                nibName:"CustomCameraViewController",
                bundle: nil
            )
            let customView:CustomCameraView = customViewController.view as! CustomCameraView

            //Assignments
            customView.frame = self.imagePicker.view.frame
            customView.delegate = self
            imagePicker.cameraOverlayView = customView
            //adjust the cameraView's position
            imagePicker.cameraViewTransform = CGAffineTransformMakeTranslation(0.0, 100.0);

            presentViewController(imagePicker,
                animated: true,
                completion: {
                }
            )
        }

If you want to scale the cameraView to take up more of the screen you can say CGAffineTransformMakeScale(scaleX,scaleY) but you will then have to crop the resulting image to match what has been shown to the user.

beyowulf
  • 15,101
  • 2
  • 34
  • 40
  • Thanks for your time. This answer really helped me to understand the process. So I assume, I probably make use of both CGAffineTransformMakeTranslation and CGAffineTransformMakeScale. I actually did try to scale and it shrank the size as I wanted. Just a quick question if you let me. When I scale down, actually user see less but it takes photo at the same size as it used to be, is that right? – Kutay Demireren Feb 29 '16 at 22:15
  • Yes. You are only scaling the preview not the actual image. – beyowulf Feb 29 '16 at 22:34