I'm using this code to create a UIImagePickerController with a custom cameraOverlayView. For testing purposes, the overlay's just supposed to be a circle that I draw in CircleView using a UIBezierPath. However, the circle drawn in the CircleView is not appearing in the overlay. The overlay is supposed to be a subclass of UIView which my CircleView is. Can you explain why the circle is not appearing when the overlay pops up? I initialize the circleView in viewDidLoad and then set it to be the cameraOverlayView in viewDidAppear.
viewController
#property (strong, nonatomic) CircleView *overlay;
viewDidLoad
self.overlay = [[CircleView alloc] init];
viewDidAppear
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.showsCameraControls = NO;
picker.navigationBarHidden = YES;
picker.wantsFullScreenLayout = YES;
picker.cameraOverlayView = self.overlay;
picker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
[self presentViewController:picker animated:YES completion:NULL];
}
CircleView
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
self.shape = [UIBezierPath bezierPathWithOvalInRect:(CGRectMake(0, 50, 50, 50))];
}
return self;
}
- (void)drawRect:(CGRect)rect{
[[UIColor colorWithRed: (114/255.0) green: (5/255.0) blue:(46/255.0) alpha:1] setFill];
[self.shape fill];
}
There are two problems here:
Your circle overlay view has zero size, because you create it with init instead of initWithFrame: (init falls back on a frame of {0,0,0,0}, because you have provided no frame information).
Your circle overlay view does some important initialization in initWithCoder:, but this method is never called, since you are instantiating it with init instead. Thus its self.shape is nil and nothing is drawn in its drawRect:.
Related
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.
Im making iPhone app using frame, my frame is not in center and it repeats. I want to set in center and I hate when my frames repeats multiple times.
Im using following code:
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
// picker.delegate = self;
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
// creating overlayView
UIView* overlayView = [[UIView alloc] initWithFrame:picker.view.frame];
// letting png transparency be
overlayView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"f2.png"]];
[overlayView.layer setOpaque:NO];
overlayView.opaque = NO;
picker.showsCameraControls = NO;
picker.cameraOverlayView = overlayView;
//[self presentModalViewController:picker animated:YES];
[self presentViewController:picker animated:YES completion:nil];
Am I missing something ?
TNX in advance.
I'm not sure, what you are asking, but maybe changing from frame to bounds could solve you problem:
UIView* overlayView = [[UIView alloc] initWithFrame:picker.view.bounds];
I am loading a UIPopoverController that contains a UIImagePickerController, when the view loads it loads a seethrough view behind the popover, which if touched it dismisses the popover and the the cameraview inside it.
I would like to know if there is a way to remove this background view and control the dismiss myself with buttons.
My goal is to have some information to the left and the camera popover view to the right, but I cannot do this as some of the information is selectable i.e. textfields, and currently this background view is preventing selection of them.
This is my code for the popovercontroller and the UIImagePickerController.
- (void) cameraButtonSelected
{
// set background
UIView *cameraBG = [[UIView alloc] initWithFrame:CGRectMake(0.0, 20.0, screenHeight, screenWidth-64)];
cameraBG.backgroundColor = [UIColor blackColor];
cameraBG.alpha = 0.8;
[self.view addSubview:cameraBG];
// create picker
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
CGFloat scaleFactor=1.3f;
picker.cameraViewTransform = CGAffineTransformScale(CGAffineTransformMakeRotation(M_PI * -90 / 180.0), scaleFactor, scaleFactor);
}
// create popover
self.popOver = [[UIPopoverController alloc] initWithContentViewController:picker];
[self.popOver presentPopoverFromRect:CGRectMake(500, 100, 0, 0) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:NO];
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
viewController.preferredContentSize = CGSizeMake(805, 650);
}
You can just call,
self.popOver.hidden = YES; or self.popOver.hidden = NO;
If you want the pop0ver view to still be drawn but still hidden to the user, you can call self.pop0ver.alpha = 0.0f;
Whenever I run my app on a device and I choose to take a picture it crashes without warning can someone tell me what is wrong with my overlay?
CameraOverlay.h does not contain anything besides making UIView the superclass.
CameraOverlay.m
#implementation CameraOverlay
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.opaque = NO;
self.backgroundColor = [UIColor clearColor];
// load an image to show in the overlay
UIImage *constraints = [UIImage imageNamed:#"camera_overlay"];
UIImageView *constraintView = [[UIImageView alloc]initWithImage:constraints];
constraintView.frame = CGRectMake(30, 100, 260, 200);
[self addSubview:constraintView];
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
viewController.m
- (IBAction)scanButton:(UIButton *)sender
{
CGRect screenRect = [[UIScreen mainScreen]bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
CameraOverlay *overlay = [[CameraOverlay alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.cameraOverlayView = overlay;
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
I found out what was wrong, I was setting the overlay of the camera before I set the source to the camera.
Updated code from the ViewController
- (IBAction)scanButton:(UIButton *)sender
{
CGRect screenRect = [[UIScreen mainScreen]bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
CameraOverlay *overlay = [[CameraOverlay alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.cameraOverlayView = overlay;
picker.delegate = self;
picker.allowsEditing = YES;
[self presentViewController:picker animated:YES completion:NULL];
}
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];
}