xCode Creating UIImagePickerController and Referencing It - ios

for my app I want to create a custom camera overlay and create a UIImagePickerController to start recording video. (below)
- (IBAction)RecordVideo:(UIButton *)sender {
//initialise camera view
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraUI.cameraDevice = UIImagePickerControllerCameraDeviceFront;
cameraUI.showsCameraControls = NO;
cameraUI.navigationBarHidden = YES;
cameraUI.toolbarHidden = YES;
OverlayView *overlay = [[OverlayView alloc]
initWithFrame:CGRectMake(0, 0, 20,20)];
cameraUI.cameraOverlayView = overlay;
[self presentViewController:cameraUI animated:YES completion:nil];
}
as part of the cameraOverlayView I've instantiated a UIButton:
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
//clear the background color of the overlay
self.opaque = NO;
self.backgroundColor = [UIColor clearColor];
... some overlay UI stuff
UIButton *captureBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIImage *captureBtnImg = [UIImage imageNamed:#"captureBtn.png"];
[captureBtn setBackgroundImage:captureBtnImg forState:UIControlStateNormal];
captureBtn.frame = CGRectMake(self.frame.size.width/2 - 15,
430,
80,
80);
[self addSubview:captureBtn];
}
return self;
}
So how do I create an IBAction for this button which can then make the UIImagePickerController cameraUI start recording?
If somebody could also explain to me how the IBActions are called from specific buttons only that would also be amazing because that seems like black magic to me?

You can add target like this,
[captureBtn addTarget:self action:#selector(captureBtnclick:) forControlEvents:UIControlEventTouchUpInside];
and you action method shoulbe like this
-(void)captureBtnclick :(UIButton*)sender{
//handle your task on click here
}
second thing why you are adding layer on it? you can use default video recording from imagePicker by setting mediatype like
cameraUI.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
Update accroding to comment
you can use on button click,
[picker startVideoCapture]; //in your case cameraUI i think
Update 2 :
in your .h file,
#property UIImagePickerController *cameraUI;
then in your method RecordVideo use like,
self.cameraUI = [[UIImagePickerController alloc] init];
self.cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
//etc.....
By this way you can use camaraUI in whole class by self
hope this will help :)

Related

capture image is not working properly.?

Hi I am very new to developing iOS application please help me. i am facing a problem regarding capturing image by using UIImagePicker. I have integrated hip mob app in to our application.
In that window i want to show a UIButton so that the user click on that UIButton should capture the image. when i click on the button its throwing an message as (whose view is not in the window hierarchy!).
I think this happens due to this chat window. here is my code snippet.!
[[HMService sharedService] openChat:self withSetup:^(HMChatViewController * controller)
{
controller.chatDelegate = self;
controller.chatView.receivedMessageFont = [UIFont systemFontOfSize:20.0];
controller.navigationBarHidden = YES;
UIView * viewObjForVehicleDetails = [[UIView alloc]init];
UIButton * btnForCaputrePhoto = [[UIButton alloc]init];
[btnForCaputrePhoto addTarget:self action:#selector(CapturePhotoImage) forControlEvents:UIControlEventTouchUpInside];
btnForCaputrePhoto.frame = CGRectMake(55, 90, 103, 85);
[controller.chatView addSubview:viewObjForVehicleDetails];
[viewObjForVehicleDetails addSubview:btnForCaputrePhoto];
[[controller.chatView table] registerClass:[ChatTableViewCell class] forCellReuseIdentifier:#"ReuseChatTableViewCell"];
}
-(void)CapturePhotoImage
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:NO completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Thanks in Advance.!
First be sure you are using the following delegate in your view controller
<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
Secondly use this line to show the ImagePicker:-
[self presentModalViewController:picker animated:YES];
Alternative Way:-
[self.view addSubview:imagePicker.cameraOverlayView]
Change the method declaration as follows
- (IBAction) CapturePhotoImage:(id)sender
{
NSLog("Called");
}
And the method call "CapturePhotoImage" as CapturePhotoImage:
[btnForCaputrePhoto addTarget:self action:#selector(CapturePhotoImage:) forControlEvents:UIControlEventTouchUpInside];

How to open front/back camera at the same time in iOS developing?

I'd like to use the front and back camera at the same time in iOS developing:when I take one photo using back camera,after that the front camera could be opened and take another photo.By the way,this two photos are in the one picture:Vertical arrangement.
Is anybody has done this before?
Use UIImagePickerController to take the pictures, and use the cameraDevice property to determine which camera is in use.
For merging images I found an example via searching(recommend it in the future) merges multiple images into one image
Super simple this is the an easy way of doing it. Just replace my imagNamed with you images and the selector is the new methods that were created. This is used for a custom camera by removing showsCameraControls = NO;
#interface yourClassName () {
UIImagePickerController *picker; //this calls the video/photo screen
UIButton *cameraFront, *cameraBack //front and back buttons
}
//in your UIImagePickerController
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
picker.showsCameraControls = NO;
[cameraFront setBackgroundImage:[UIImage imageNamed:#"camera_switch_logo"] forState:UIControlStateNormal];
UITapGestureRecognizer *camerafront = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(deviceModeFront:)];
[cameraFront addGestureRecognizer:camerafront];
[cameraBack setBackgroundImage:[UIImage imageNamed:#"camera_switch_logo2"] forState:UIControlStateNormal];
UITapGestureRecognizer *cameraback = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(deviceModeBack:)];
[cameraBack addGestureRecognizer:cameraback];
cameraBack.hidden = true;
//These are the new methods created as the selector for when image is pressed
- (IBAction)deviceModeFront:(id)sender {
[picker setCameraDevice:UIImagePickerControllerCameraDeviceFront];
cameraFront.hidden = true;
cameraBack.hidden = false;
}
- (IBAction)deviceModeBack:(id)sender {
[picker setCameraDevice:UIImagePickerControllerCameraDeviceRear];
cameraFront.hidden = false;
cameraBack.hidden = true;
}

iOS - Taking A Picture After Button Tap in UIImagePickerController CustomOverlayView

I have searched similar questions for answers, but nothing I have tried has worked. When I tap on either button in my overlay view, the camera only focuses - no action is triggered. Here is my code:
Update: The previous question was answered - the code was fine, I just wasn't alerting myself in the testIfButtonResponds method. Now I am wondering how to dismiss the camera and show the captured picture in a UIImageView.
Also, the UIImageView *DisplayedPhoto was made in IB, so it has a frame.
#interface CameraViewController ()
#property UIImagePickerController *PickerController;
#property (weak, nonatomic) IBOutlet UIImageView *DisplayedPhoto;
#end
- (UIView *)createCustomOverlayView
{
// Main overlay view created
UIView *main_overlay_view = [[UIView alloc] initWithFrame:self.view.bounds];
// Clear view (live camera feed) created and added to main overlay view
// ------------------------------------------------------------------------
UIView *clear_view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - self.HeightOfButtons)];
clear_view.opaque = NO;
clear_view.backgroundColor = [UIColor clearColor];
[main_overlay_view addSubview:clear_view];
// ------------------------------------------------------------------------
// Creates the buttons on the bottom of the view (on top of the live camera feed)
// Then adds the buttons to the main overlay view
// ------------------------------------------------------------------------
for(int i = 0; i < 2; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
// when a button is touched, UIImagePickerController snaps a picture
[button addTarget:self action:#selector(testIfButtonResponds) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake( i * self.view.frame.size.width / 2, self.view.frame.size.height - self.HeightOfButtons, self.view.frame.size.width / 2, self.HeightOfButtons);
[button setBackgroundColor:[UIColor redColor]];
[main_overlay_view addSubview:button];
}
// ------------------------------------------------------------------------
return main_overlay_view;
}
- (void)makeCustomCameraAppear
{
self.PickerController = [[UIImagePickerController alloc] init];
self.PickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
self.PickerController.showsCameraControls = NO;
self.PickerController.delegate = self;
UIView *overlay_view = [self createCustomOverlayView];
[self.PickerController setCameraOverlayView:overlay_view];
[self presentViewController:self.PickerController animated:YES completion:NULL];
}
- (void)viewDidAppear:(BOOL)animated
{
[self makeCustomCameraAppear];
}
- (void) testIfButtonResponds
{
[self.PickerController takePicture];
[self.PickerController dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *chosen_image = info[UIImagePickerControllerEditedImage];
self.DisplayedPhoto.image = chosen_image;
[self.PickerController dismissViewControllerAnimated:YES completion:NULL];
}

How to disable UITapGestureRecognizer when sellect photo from library?

I'm using UITapGestureRecognizer
This is my code:
Home.m:
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapAnim:)];
[self.view addGestureRecognizer:tapGesture];
UIButton *buttontest = [[UIButton alloc] init];
buttontest.backgroundColor = [UIColor whiteColor];
buttontest.frame = CGRectMake(0, 80, 40, 40);
[buttontest addTarget:self action:#selector(test:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttontest];
[self.view bringSubviewToFront:buttontest];
}
- (void)test: (UIButton*)aButton {
// TakePhoto *mvc = [[TakePhoto alloc]initWithNibName:#"TakePhoto" bundle:Nil];
// [self.navigationController pushViewController:mvc animated:YES];
//
// [self.view removeFromSuperview];
if (self.companyController) {
self.companyController = nil;
}
self.companyController = [[TakePhoto alloc] initWithNibName:#"TakePhoto" bundle:nil];
UIView *viewSuper = [[IQAppDelegate shareInstance] currentVisibleController].view;
UIViewController *profile = self.companyController;
profile.view.frame = viewSuper.frame;
[viewSuper addSubview:profile.view];
profile.view.frame = CGRectMake(viewSuper.frame.origin.x, viewSuper.frame.size.height, profile.view.frame.size.width, profile.view.frame.size.height);
[UIView beginAnimations:nil context: nil];
[UIView setAnimationDuration:0.35];
profile.view.frame = CGRectMake(viewSuper.frame.origin.x, viewSuper.frame.origin.x, profile.view.frame.size.width, profile.view.frame.size.height);
[UIView commitAnimations];
}
}
- (void) tapAnim: (UITapGestureRecognizer*)gestureRecognizer {
// Show something
}
TakePhoto.m
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
picker.allowsEditing = NO;
[(UIViewController *)self.delegate presentModalViewController:picker animated:YES];
I add a view:Takephoto front of Home(i'm not use "push"), like this:
--->Home
--->Take photo (like a popUp show): it has 2 buttons "Choose photo from library" and "Close"
When i use function "Choose photo from gallery", i cant choose a photo and UITapGestureRecognizer is always show.
How to disable UITapGestureRecognizer when choose photo from gallery?
P/S:sorry about my english.
Gesture recognizers have the enabled property. Set this to NO to disable the gesture recognizer. To make this easier you should keep a reference to the tap gesture recognizer using an instance variable.
you can set tapGesture.enabled=NO before you choose photos.
I think you need to implement this below delegate method :-
- (BOOL)gestureRecognizer:
(UIGestureRecognizer *)gestureRecognizer
shouldReceiveTouch:(UITouch *)touch
When you look on the documentation it will return YES (by default) to allow the gesture recognizer to examine the touch object, NO to prevent the gesture recognizer from seeing this touch object. For more details follow this https://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UIGestureRecognizerDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIGestureRecognizerDelegate/gestureRecognizer:shouldReceiveTouch:

UIImagePickerController and a small custom preview

I want to use an UIImagePickerController non-modally, with a small square preview. However, I get a big black bar at the bottom of the preview, which I can't get rid of (see the screenshot). Why is it there, and how can I get rid of it?
Code:
- (void)viewDidLoad
{
[super viewDidLoad];
_imagePickerVC = [[UIImagePickerController alloc] init];
_imagePickerVC.sourceType = UIImagePickerControllerSourceTypeCamera;
_imagePickerVC.mediaTypes = #[ (NSString *)kUTTypeImage ];
_imagePickerVC.wantsFullScreenLayout = NO;
_imagePickerVC.delegate = (id)self;
_imagePickerVC.navigationBarHidden = YES;
_imagePickerVC.allowsEditing = NO;
_imagePickerVC.showsCameraControls = NO;
_imagePickerVC.cameraDevice = UIImagePickerControllerCameraDeviceFront;
CGRect previewFrame = CGRectMake(0, 0, 150, 150);
[_imagePickerVC.view setFrame:previewFrame];
_imagePickerVC.view.autoresizesSubviews = YES;
[self.view addSubview:_imagePickerVC.view];
[_imagePickerVC viewWillAppear:YES];
[_imagePickerVC viewDidAppear:YES];
}
This is a misuse of UIImagePickerController. To make your own image-capture interface, simply use AVFoundation.

Resources