UIImagePicker that only fills the top half of the screen - ios

I'm trying to present a camera on the only the top half of my screen and my code isn't resizing the camera properly. I'm trying to add a view to the top half of my screen and then have the camera's cameraOverlayView property conform to that view's frame. Regardless of what I try however, the camera still appears in full screen mode. If someone can tell me what I'm doing wrong I'd really appreciate it. Thanks!
// Setting Up The Camera View
cameraView = UIView(frame: CGRectMake(0.0, 0.0, view.bounds.width, view.bounds.width))
view.addSubview(cameraView)
// Setting Up The Camera
var cam = UIImagePickerController()
cam.delegate = self
cam.allowsEditing = false
cam.videoMaximumDuration = 7
cam.videoQuality = UIImagePickerControllerQualityType.TypeMedium
cam.mediaTypes = UIImagePickerController.availableMediaTypesForSourceType(UIImagePickerControllerSourceType.Camera)!
cam.sourceType = UIImagePickerControllerSourceType.Camera
cam.cameraDevice = UIImagePickerControllerCameraDevice.Rear
cam.cameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Video
cam.cameraFlashMode = UIImagePickerControllerCameraFlashMode.Off
cam.showsCameraControls = true
cam.cameraOverlayView = cameraView
cam.cameraOverlayView?.frame = cameraView.frame
camera = cam
self.presentViewController(camera, animated: false, completion: nil)

I do not think what you want to do can be accomplished using a UIImagePickerController. The AVFoundation is what you want to use if you want complete customization of the camera. You can read about it here https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/04_MediaCapture.html#//apple_ref/doc/uid/TP40010188-CH5-SW2.
P.S. It is not the simplest framework to use

Related

Setting iOS UIImagePickerController to Landscape mode for external display

I'm creating an app for an art installation which will mimic Apple's own camera app, but with slight modifications (slight video display parameters, front camera only, etc.). Everything is working for the most part, however I will have the camera connected to an external monitor, and therefore need it to display in landscape mode, not portrait mode. I have it connected via Lightning to HDMI.
I am using UIImagePickerController and not AVFoundation, because by default it does face detection which is identical to Apple's own (the yellow squares around the faces). I get the feeling that replicating the face detection in AV foundation would be a significant amount of trouble. However, UIImagePickerController simply refuses to display in landscape mode. This is the message in the reference about it:
The UIImagePickerController class supports portrait mode only. This class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified, with one exception. You can assign a custom view to the cameraOverlayView property and use that view to present additional information or manage the interactions between the camera interface and your code.
from: https://developer.apple.com/reference/uikit/uiimagepickercontroller
Is what I want to do still possible? I am already using a cameraOverlayView, however that so far has not fixed the problem? Can I just change things for HDMI display? Since I will not be submitting this app to apple, I don't particularly need to follow their standards as long as I can install it on my own iPhone 7 Plus running 10.1.1.
Here is my current code in case it helps:
let imagePicker = UIImagePickerController()
let myView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 200))
myView.backgroundColor = UIColor(colorLiteralRed: 0.5, green: 1.0, blue: 0.2, alpha: 1)
myView.alpha = 0.1
imagePicker.delegate = self
imagePicker.sourceType =
UIImagePickerControllerSourceType.camera
imagePicker.cameraDevice = UIImagePickerControllerCameraDevice.front
imagePicker.mediaTypes = [kUTTypeImage as String]
imagePicker.allowsEditing = false
imagePicker.videoQuality = UIImagePickerControllerQualityType.type640x480
//imagePicker.showsCameraControls = false
imagePicker.cameraOverlayView = myView
imagePicker.modalPresentationStyle = UIModalPresentationStyle.currentContext
self.present(imagePicker, animated: true,
completion: nil)

Whats the `allowsEditing` flag good for?

I wanted to use UIImagePickerController to select an image, crop it to a given size (using a given aspect ratio) and to set it on my UIImageView.
So I did the following:
let picker = UIImagePickerController();
picker.modalPresentationStyle = .currentContext;
picker.allowsEditing = true;
picker.delegate = self;
self.present(picker, animated: true);
When I know try it I can select an image and I get leaded to the "move and scaling" view of iOS. But actually.... I have no crop frame and also moving and scaling is a little bit "strange" and it does not matter how I move or scale the image, the result is always the same. The result is always just showing a small section of the original picture (top left corner).
So what is the editing mode good for at all if it has zero functionality?

MPMoviePlayer view issue when coming out of full screen

I am using MPMoviePlayerController for showing video inside UIView and using the code below:
self.moviePlayerSmall = MPMoviePlayerController(contentURL:self.objUrl )
self.moviePlayerSmall.view.frame = self.videoView.bounds
self.videoView.addSubview(self.moviePlayerSmall.view)
self.moviePlayerSmall.view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
for subV: UIView in self.moviePlayerSmall.view.subviews {
subV.backgroundColor = UIColor.whiteColor()
}
self.moviePlayerSmall.fullscreen = false
self.moviePlayerSmall.controlStyle = MPMovieControlStyle.Default
self.moviePlayerSmall.scalingMode = .AspectFill
Where videoview is the UIView in which I am adding the MPMoviePlayer view.
The issue I am facing is that whenever the video player comes out of full screen then the player view shows spacing on the left and right sides.
I have tried setting up contentmode, ScaleMode and even AutorezingMask but nothing is working. Have anyone experienced the same issue?

Video stretch issue iOS Swift

I have an application with Swift. I want to play some video by using moviePlayer. I am using the following code.
let url = NSURL.fileURLWithPath(path)
moviePlayer = MPMoviePlayerController(contentURL: url)
// moviePlayer?.controlStyle = .None
if let player = moviePlayer {
player.view.frame = CGRect(x: 20, y: 165, width: widthVideoView, height: heightVideoView)
player.prepareToPlay()
player.scalingMode = .AspectFill
self.view.addSubview(player.view)
}
Playing video is fine no issues. But for now my video is stretched . I think this is happening because of
player.scalingMode = .AspectFill
My screenshot like this. .
So I change to
player.scalingMode = .AspectFit
Then my screen like this (top bottom with black screen.) How can I handle this.
Sorry for the edited answer , If you read the docu : https://developer.apple.com/library/prerelease/ios/documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/index.html#//apple_ref/c/tdef/MPMovieScalingMode
.aspectFit: will create the blackBar because it keeps the aspect ratio of the video
.aspectFill , will act like a zoom on the (there is no black bar but the video get cut)
.Fill will stretch the video to fit (I think you want this one or change the view to follow the aspect ratio ).
If you don't respect the original aspect ratio :It will always stretch or have the black bars too fill .
So you can change the scalingMode to .Fill or change the view to respect the aspect ratio

Camera with Custom View

My Application uses camera, I would like to add overlay over the camera preview. For example, I want to use a picture frame when I use Camera, also I would like to add a custom bar for camera operations. Kindly help me to do the same.
You might be trying using UIImagePickerController. But I know this one solution to your problem. You can do it easily using AVCamCaptureManager and AVCamRecorder classes. Apple has a demo program build on its developer site here. It is named AVCam. In simple words what it does is when you click to open the camera, it calls the classes and methods which are responsible for opening the iPhone's camera and record video or capture audio. It calls the same classes which are called by UIImagePickerController. So your camera will open and start taking input.
Now, if you open the xib file of that AVCam project, you'll find a small UIView object. This view is responsible for displaying the camera's feed. You can resize that view as per the size you want and the camera's input will be displayed in that much area. You can also put the frame image around it as per your choice.
It worked for me when I wanted to resize the camera's input feed and capture photos. I hope it works for you as well.
Create a UIImagePickerController from code, adjust its properties, add an overlay onto it, and with you controller, control whatever you want on that overlay : custom controls, overlaying images, etc...
That gives something like this :
self.picker = [[UIImagePickerController alloc] init];
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
self.picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
self.picker.showsCameraControls = NO;
self.picker.navigationBarHidden = YES;
self.picker.toolbarHidden = YES;
self.picker.wantsFullScreenLayout = YES;
// Insert the overlay
self.overlay = [[OverlayViewController alloc] initWithNibName:#"Overlay" bundle:nil];
self.overlay.pickerReference = self.picker;
self.picker.cameraOverlayView = self.overlay.view;
self.picker.delegate = self.overlay;
[self presentModalViewController:self.picker animated:NO];
OverlayViewController is the controller that you must write to control everything you add onto the overlay.
pickerReference is a property you can keep to send orders to the camera. For example, you could call the following from an IBAction coming from a UIButton placed onto the overlay :
[self.pickerReference takePicture];
Read the UIImagePickerController Class Reference, that's right in the documentation…
There are properties for this, especially the cameraOverlayView and showsCameraControls properties.
So you can hide the controls, provide a custom overlay view, and add subviews to this custom view to add custom buttons, frames, etc.
Swift 3 version for answer from Oliver:
self.picker = UIImagePickerController()
self.picker.sourceType = .camera
self.picker.cameraCaptureMode = .photo
self.picker.cameraDevice = .rear
self.picker.showsCameraControls = false
self.picker.isNavigationBarHidden = true
self.picker.isToolbarHidden = true
// Insert the overlay
self.overlayViewController = self.storyboard?.instantiateViewController(withIdentifier: "Overlay") as! OverlayViewController
self.picker.cameraOverlayView = self.overlayViewController.view
self.picker.delegate = self.overlayViewController
self.navigationController?.present(self.picker, animated: true, completion: nil)
OverlayViewController protocols:
class OverlayViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate

Resources