set cameraOverlayView with xib - ios

I created a simple xib file with a UIView and a UILabel in the middle of the view. I then set the owner of the xib to be the ViewController that will be using it. I created an outlet from the UIView to my ViewController and did the following:
imagePicker.cameraOverlayView = self.cameraOverlay;
The problem I am having is that it is not appearing when I open the camera. If I create the UIView programmatically it works, but it doesn't seem to work if I use a xib file.
Below is the code where I set the overlay, and camera:
- (IBAction)takeVideo:(id)sender {
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
NSArray *availableTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
if ([availableTypes containsObject:(__bridge NSString *)kUTTypeMovie]) {
[imagePicker setMediaTypes:#[(__bridge NSString *)kUTTypeMovie]];
}
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.delegate = self;
imagePicker.cameraOverlayView = self.cameraOverlay;
[self presentViewController:imagePicker animated:YES completion:nil];
}
The camerOverlay is (strong, nonatomic)

Have you loaded the nib in code?
self.cameraOverlay = [[[NSBundle mainBundle] loadNibNamed:#"YourNibNameHere" owner:self options:nil] objectAtIndex:0];
Place this in your viewDidLoad method.

Related

iOS Objective-c button pressed event from UIView as overlay for Camera

I'm fairly new to Objective-C and I'm trying to get a button pressed event to trigger on a button that is on a custom view that is being overlaid on a UIImagePicker.
I connect the button to an IBAction on the overlaid view, but when I press the button I get a bad access error.
here is the code I am using to for the overlay:
// prepare imagePicker view
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.delegate = self;
imagePicker.allowsEditing = NO;
imagePicker.showsCameraControls = NO;
// create view for overlay
CGRect overlayRect = CGRectMake(0, 0, imagePicker.view.frame.size.width, imagePicker.view.frame.size.height);
UIView *overlayView = [[UIView alloc] initWithFrame:overlayRect];
// prepare custom view
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
#"Main" bundle:[NSBundle mainBundle]];
UIViewController *overlay = [storyboard instantiateViewControllerWithIdentifier:#"overlayView"];
imagePicker.cameraOverlayView = overlay.view;
// display imagePicker
[self.navigationController presentViewController:imagePicker animated:YES completion:nil];
can you try to make UIImagePickerController *imagePicker as a property
#property (nonatomic) UIImagePickerController *imagePicker;
then
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.imagePicker.delegate = self;
self.imagePicker.allowsEditing = NO;
self.imagePicker.showsCameraControls = NO;
Good luck
You should have the buttons point to a method on the presenting viewController. Maybe adding the overlay as a childViewController to the imagePicker would solve your problem, but the cameraOverlayView should be just a view.
Here's a good implementation of what you're trying to do: Using cameraOverlayView with UIImagePickerController
UPDATE: Keep a weak reference to overlayView from your presenting View Controller through a property such as:
#property (weak) OverlayView *overlayView;
and in your code:
// create view for overlay
CGRect overlayRect = CGRectMake(0, 0, imagePicker.view.frame.size.width, imagePicker.view.frame.size.height);
imagePicker.cameraOverlayView = [[OverlayView alloc] initWithFrame:overlayRect];
// Hold a weak reference to the `cameraOverlayView`
self.overlayView = (OverlayView *)imagePicker.cameraOverlayView;
now the presenting viewController can access self.overlayView's public properties when the imagePicker is alive. Make sure you check if self.overlayView before trying to access its properties.

cameraOverlayView from xib

I have been searching for a while and can not seem to find a clear way of doing this. Any link I did find is outdated. I am presenting a camera like so
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//Get the camera
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
self.imagePicker.allowsEditing = NO;
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];
self.imagePicker.showsCameraControls = NO;
self.imagePicker.wantsFullScreenLayout = YES; //<-- This does not work in iOS7
//set our custom overlay view
[self presentViewController:self.imagePicker animated:NO completion:nil];
}
I have a ViewController connected to an xib named ControllsViewController . I have no idea how to assign it to self.imagePicker.cameraOverlayView = . Can anyone help me here?
You need to instantiate the ControllsViewController using the xib. Assuming you have a xib file called "ControllsViewController" in your main bundle:
ControllsViewController *overlayViewController = [ControllsViewController alloc] initWithNibName:#"ControllsViewController" bundle:nil];
self.imagePicker.cameraOverlayView = overlayViewController.view;
You probably want to assign overlayViewController to a property to keep it around.

Make a UIView display the camera view using UIImagePickerController

I'm trying to display the image a the camera sees into my own custom view. I DO NOT want to use Apple's own overlay property for UIImagePickerController: cameraOverlayView.
I don't want an overlay, but for it to just display in my particular view. The only way I've been able to do it was creating a subview in my UIView, but doing this doesn't allow me to use any of my own custom buttons I've made over my UIView . My code so far is:
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
[imagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera];
[imagePickerController setShowsCameraControls:NO];
[imagePickerController setEditing:NO];
[imagePickerController setNavigationBarHidden:YES];
[self.videoPreviewView addSubview:[imagePickerController view]];
[imagePickerController viewWillAppear:YES];
where videoPreviewView is the outlet for my UIView!
Any suggestions? Thanks!
I have not tried it your way myself, but I also wanted to create a custom camera view. This might not be what you are looking for, but it is an alternative. I created a custom UIView together with .xib file.
In the .m file you just call the appropriate delegate methods to the ViewController where you instantiated the imagePicker. In this ViewController you then call appropriate methods like takePicture.
This way you can make the overlay look any way you like with the xib file. You can instantiate the image picker
FALKCameraView.h :
#protocol CameraViewDelegate <NSObject>
- (void)cameraPressed;
- (void)buttonPressed;
//etc
#end
#interface FALKCameraView : UIView
#property (nonatomic, assign) NSObject<CameraViewDelegate> *delegate;
//Camera Button pressed- notifies CameraViewDelegate
- (IBAction)cameraPressed:(id)sender;
//Button pressed - notifies CameraViewDelegate
-(IBAction)buttonPressed:(id)sender;
#end
FALKCameraView.m:
#implementation FALKCameraView
#synthesize delegate;
- (IBAction)cameraPressed:(id)sender{
[delegate cameraPressed];
}
-(IBAction)buttonnPressed:(id)sender{
[delegate buttonPressed];
}
#end
FALKCameraViewController.h:
#interface FALKCameraViewController : UIViewController <UIImagePickerControllerDelegate, CameraViewDelegate>
- (void)cameraPressed;
- (void)buttonPressed;
#end
FALKCameraViewController.m:
#interface FALKCameraViewController
#property (strong,nonatomic) UIImagePickerController *imagePicker;
#end
#implementation FALKCameraViewController
#synthesise imagePicker;
-(void)setUpCamera{
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera]){
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypeCamera;
imagePicker.showsCameraControls = NO;
imagePicker.allowsEditing = NO;
imagePicker.mediaTypes = #[(NSString *) kUTTypeImage];
NSString *nibName = #"FALKCameraView";
NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:nibName
owner:nil
options:nil];
if([nibs count]){
id nib = [nibs objectAtIndex:0];
if([nib isKindOfClass:[FALKCameraView class]]){
FALKCameraView *view = (FALKCameraView*)nib;
view.delegate = self;
imagePicker.cameraOverlayView = view;
}
}
[self presentViewController:imagePicker
animated:NO completion:nil];
}
}
-(void)cameraPressed:(id)sender{
[imagePicker takePicture];
}
#end
Hope it will work and i just added a single view you can add any custom object to overlayview of the picker
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera]){
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypeCamera;
imagePicker.showsCameraControls = NO;
imagePicker.allowsEditing = NO;
NSString *nibName = #"CameraView";
NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:nibName
owner:nil
options:nil];
if([nibs count]){
id nib = [nibs objectAtIndex:0];
if([nib isKindOfClass:[CameraView class]]){
CameraView *view = (CameraView*)nib;
view.delegate = self;
imagePicker.cameraOverlayView = view;
}
}
[self presentViewController:imagePicker
animated:NO completion:nil];
}

UIPopoverController for iPad "must not be called with `nil`" error

I am trying to make an iPhone app work in an iPad but the UIPopoverController is
comming back with error.
- (IBAction)photoTapped1 {
if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){
// If in editing state, then display an image picker; if not, create and push a photo view controller.
if (self.editing) {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
[self presentViewController:imagePicker animated:YES completion:nil];
[imagePicker release];
} else {
RecipePhotoViewController *recipePhotoViewController = [[RecipePhotoViewController alloc] init];
recipePhotoViewController.hidesBottomBarWhenPushed = YES;
recipePhotoViewController.recipe = recipe;
[self.navigationController pushViewController:recipePhotoViewController animated:YES];
[recipePhotoViewController release];
}
}else{
if (self.editing){
self.popover = [[UIPopoverController alloc] initWithContentViewController:popover];
self.popover.delegate =self;
[popover release];
}else{
RecipePhotoViewController *recipePhotoViewController = [[RecipePhotoViewController alloc] init];
recipePhotoViewController.hidesBottomBarWhenPushed = YES;
recipePhotoViewController.recipe = recipe;
[self.navigationController pushViewController:recipePhotoViewController animated:YES];
[recipePhotoViewController release];
}}}
The error I am getting is:
'NSInvalidArgumentException', reason: '-[UIPopoverController initWithContentViewController:] must not be called with nil.'
Anyone available to give me a hand on this code, I have looked on the internet for solutions and samples but can not seem to make it work.
Thank you.
___ added to original question_____
I am adding the recipePhotoViewController here, I am assuming that ImageView manipulation is the same for iPhone and iPad.
my.h File
#class Recipe;
#interface RecipePhotoViewController : UIViewController {
#private
Recipe *recipe;
UIImageView *imageView;
}
#property(nonatomic, retain) Recipe *recipe;
#property(nonatomic, retain) UIImageView *imageView;
#end
Here is my .m file
#implementation RecipePhotoViewController
#synthesize recipe;
#synthesize imageView;
- (void)loadView {
self.title = #"Photo";
imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.backgroundColor = [UIColor blackColor];
self.view = imageView; }
- (void)viewWillAppear:(BOOL)animated {
imageView.image = [recipe.image valueForKey:#"image"];}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)dealloc {
[imageView release];
[recipe release];
[super dealloc];
} #end
You are initializing the popover controller in a wrong way:
self.popover = [[UIPopoverController alloc] initWithContentViewController:popover];
You should pass the controller you would like to display inside of the popover -- not the popover itself (which is nil since you have not yet initialised it)!
Maybe this would do it for you?
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
self.popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
If this is correct, then you should also present the pop over you have just created: – presentPopoverFromRect:inView:permittedArrowDirections:animated:
-- e.g.:
[self.popover presentPopoverFromRect:sender.frame inView:[self view] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
where sender is the argument to :
- (IBAction)photoTapped1:(id)sender {
The content of your popover which you store in the variable popover is obviously nil. I can't see it ever being created in the code you provided.
Maybe you intended to present the Recipe photo controller as the content of the popover. In that case you would do something like
RecipePhotoViewController *recipePhotoViewController = [[RecipePhotoViewController alloc] init];
self.popover = [[UIPopoverController alloc] initWithContentViewController:recipePhotoViewController];
problem is in this line of your code ,
self.popover = [[UIPopoverController alloc] initWithContentViewController:popover];
try like this
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 400, 320, 260)];
popoverContent.view = popoverView;
//Add what ever you want popoverView
self.popover = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
self.popover.delegate =self;

how to open an image picker on ipad just in code

How can I create the image picker in code?
I use iOS 6.0 , with ARC, for ipad.
I would like to able to select the picture and somehow get UIImage of the selected image.
i did add delegates:
enter code here
in the viewDidLoad method did
enter code hereimagePicker = [[UIImagePickerController alloc] init];
and in the button method for click i have put
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePicker animated:YES];
the crash happens in
[self presentModalViewController:imagePicker animated:YES];
There are some tutorials here http://www.raywenderlich.com/130/how-to-write-a-custom-image-picker-like-uiimagepicker and http://mobileorchard.com/ios-advanced-programming-the-image-picker/ that might help you
I have similar code in one of my projects, but I had
[self presentViewController:imagePicker animated:YES completion:nil];
instead of
[self presentModalViewController:imagePicker animated:YES];
Try that and see if it works. Note the presentViewController rather than presentModalViewController.
Add a property in .h
#property (strong) UIPopoverController *pop;
In you r .m file under your button implimentation add something like:
if (self.pop) {
[self.pop dismissPopoverAnimated:YES];
}
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.delegate = self;
imagePickerController.allowsEditing = YES; //if you want to edit the image
self.pop=[[UIPopoverController alloc] initWithContentViewController:imagePickerController];
//choose the direction of the arrow for the popover
[self.pop presentPopoverFromRect:((UIButton *)sender).bounds inView:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
Make sure you have your <UIPopoverControllerDelegate> delegate set in .h

Resources