What is wrong with my camera overlay iOS 7? - ios

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];
}

Related

View shifted across to the right

I am trying to figure out why this tableView is shifted across to the right, it appears in every view the app uses, yet the app itself displays fine, it's only when the tableView appears is everything shifted out of the screen.
I believe the initializing is here;
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"Lets load the frame");
CGRect frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view setFrame: frame];
self.contentSizeForViewInPopover = CGSizeMake(320, 480);
m_mywork = nil;
dataArray = [LoadSessionController loadTableData:YES];
}
Here is the initializing from another class;
on iPad display is perfect.
- (void) ShowInitialMenu
{
InitialMenuController *MewMgrController = [[InitialMenuController alloc] initWithStyle:UITableViewStyleGrouped];
MewMgrController.delegate = self;
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController: MewMgrController];
if(![ColorSplashAppDelegate isiPad]){
[self presentViewController:navController animated:YES completion:nil];
}
else{
UIPopoverController* popover = [[UIPopoverController alloc] initWithContentViewController:navController];
popover.delegate = self;
[popover presentPopoverFromRect:CGRectMake(30.0, 940.0, 30.0, 480.0)
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionDown
animated:YES];
ColorSplashAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
appDelegate.popoverController = popover;
}
[navController release];
[MewMgrController release];
[self enableUserAction:YES];
}
Try This.
-(void)viewDidLayoutSubviews
{
CGRect frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view setFrame: frame];
self.contentSizeForViewInPopover = CGSizeMake(320, 480);
m_mywork = nil;
}

How to set frame in center xcode

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];

drawing image in cameraOverlayView

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:.

Running camera on iPhone (OS 7)

I have code that perfectly works on iPad
self.clockInEmployee = nil;
self.clockInEmployee = [[userInfo userInfo] valueForKey:#"employee"];
self.clockInEmployeeRole = [[userInfo userInfo] valueForKey:#"role"];
CLog (#"PIN %#", self.clockInEmployee.pin);
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = (id)self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.showsCameraControls = YES;
picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
picker.cameraOverlayView = self.cameraOverlay.view;
if (isPhone())
{
picker.view.frame = CGRectMake(0, 748, 1024, 748);
[mainNavController.view addSubview:picker.view];
[UIView animateWithDuration:0.5 animations:^{
picker.view.frame = CGRectMake(0, 0, 1024, 748);
} completion:^(BOOL finished){
[picker viewDidAppear:YES];
}];
}
modViewController = picker;
but on iPhone when it try to launch camera I only observe black screen.
But if I try to launch it like that:
[appPresentingViewController presentViewController:picker animated:YES completion:nil];
it launches successfully. But in my particular project it causes some UI problems, so it can't be used.
How can I make this
if (isPhone())
{
picker.view.frame = CGRectMake(0, 748, 1024, 748);
[mainNavController.view addSubview:picker.view];
[UIView animateWithDuration:0.5 animations:^{
picker.view.frame = CGRectMake(0, 0, 1024, 748);
} completion:^(BOOL finished){
[picker viewDidAppear:YES];
}];
}
work for both iPad and iPhone ?
Please try this:-
//create an overlay view instance
OverlayView *overlay = [[OverlayView alloc]
initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH)];
//create a new image picker instance
UIImagePickerController *picker =
[[UIImagePickerController alloc] init];
//set source to video!
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
//hide all controls
picker.showsCameraControls = NO;
picker.navigationBarHidden = YES;
picker.toolbarHidden = YES;
//make the video preview full size
picker.wantsFullScreenLayout = YES;
picker.cameraViewTransform =
CGAffineTransformScale(picker.cameraViewTransform,
CAMERA_TRANSFORM_X,
CAMERA_TRANSFORM_Y);
//set our custom overlay view
picker.cameraOverlayView = overlay;
//show picker
[self presentModalViewController:picker animated:YES];
for further refrence -here
You may like to go through apples sample project of AVCAM it can be found here
what you need to do is something like this:
AVCaptureSession *avcam = [[AVCaptureSession alloc] init];
AVCaptureVideoPreviewLayer *previewLayer =
[AVCaptureVideoPreviewLayer layerWithSession:avcam];
previewLayer.frame = self.previewView.bounds;
[self.previewView.layer addSublayer:previewLayer];

easier way to subview a UIImagePickerController? mines not working

When I run my code and launch the camera with the code below I get my custom overlayView along with the default camera buttons. The only problem is, now, I get a blank black box where my UIImagePicker once was. I am simply trying to create a camera using the default UIImagePicker controls and buttons and then add my own IBOutlet UILabel over top of the default imagePickerController so that my label is displayed along with all the default camera controls. I'm having a terrible time accomplishing this though. cheers!
- (void)viewDidLoad
{
[super viewDidLoad];
if (self.image == nil && [self.videoFilePath length] == 0) {
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
self.imagePicker.allowsEditing = NO;
self.imagePicker.videoMaximumDuration = 10;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];
[[NSBundle mainBundle] loadNibNamed:#"OverlayView" owner:self options:nil];
self.overlayView.frame = self.imagePicker.cameraOverlayView.frame;
self.imagePicker.cameraOverlayView = self.overlayView;
self.overlayView = nil;
}
[self presentViewController:self.imagePicker animated:YES completion:nil];
}
Try to use my code. It worked fine. May be you achieve what you want. I created a controller, and used it's view to set cameraOverlayView property. I didn't use nib files, I created everything programmatically.
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.tabBarItem.image = [UIImage imageNamed:#"86-camera"];
self.cameraOverlayViewController = [[CameraOverlayViewController alloc] init];
self.cameraOverlayViewController.picker = picker;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.showsCameraControls = NO;
picker.toolbarHidden = YES;
picker.cameraOverlayView = self.cameraOverlayViewController.view;
picker.delegate = self.cameraOverlayViewController;
}
else if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
This is a code for custom CameraOverlayViewController:
Interface file:
#import <UIKit/UIKit.h>
#interface CameraOverlayViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
#property (nonatomic, strong) UIImagePickerController *picker;
#end
Implementation file:
#import "CameraOverlayViewController.h"
#import "BButton.h"
#import "AnalyseViewController.h"
#import "Tree.h"
#interface CameraOverlayViewController ()
{
}
#end
#implementation CameraOverlayViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.view.backgroundColor = [UIColor clearColor];
self.view.opaque = NO;
}
return self;
}
- (void)loadView
{
[super loadView];
UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bottom - 150, 320, 150)];
bottomView.backgroundColor = [UIColor blackColor];
BButton *button = [[BButton alloc] initWithFrame:CGRectMake(50, 25, 220, 50) type:BButtonTypePrimary];
[button setTitle:#"Сфотографировать" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[bottomView addSubview:button];
[self.view addSubview:bottomView];
UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 70)];
topView.backgroundColor = [UIColor clearColor];
UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 310, 60)];
infoLabel.backgroundColor = [UIColor clearColor];
infoLabel.font = [UIFont fontWithName:#"Georgia" size:18];
infoLabel.textColor = [UIColor whiteColor];
infoLabel.textAlignment = NSTextAlignmentCenter;
infoLabel.numberOfLines = 0;
infoLabel.text = #"Поместите лист дерева внутри рамки, чтобы\nсфотографировать его.";
[topView addSubview:infoLabel];
[self.view addSubview:topView];
UIView *bordersView = [[UIView alloc] initWithFrame:CGRectMake(10, topView.bottom + 10, 300, 300)];
bordersView.layer.cornerRadius = 10;
bordersView.layer.borderColor = [UIColor whiteColor].CGColor;
bordersView.layer.borderWidth = 2;
bordersView.backgroundColor = [UIColor clearColor];
[self.view addSubview:bordersView];
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
AnalyseViewController *analyseViewController = [[AnalyseViewController alloc] initWithNibName:nil bundle:nil];
analyseViewController.baseImage = image;
analyseViewController.treesArray = [Tree retrieveAllTrees];
UINavigationController *analyseNavController = [[UINavigationController alloc] initWithRootViewController:analyseViewController];
[AppDel.tabBarController presentViewController:analyseNavController animated:YES completion:nil];
}
#pragma mark - Other methods
- (void)buttonClicked:(id)sender
{
[self.picker takePicture];
}
#end
Delete unneeded classes like BButton yourself.
fixed it with this. I did some research on CGPoints & CGRect and come up with this solution. Not sure if it is the most elegant solution but it works and seems to make pretty good sense. - (void)viewDidLoad
{
[super viewDidLoad];
if (self.image == nil && [self.videoFilePath length] == 0) {
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
self.imagePicker.allowsEditing = YES;
self.imagePicker.videoMaximumDuration = 10;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];
[self presentViewController:self.imagePicker animated:NO completion:nil];
}
[[NSBundle mainBundle] loadNibNamed:#"OverlayView" owner:self options:nil];
self.overlayView.frame = CGRectMake(160,0,0,0);
self.imagePicker.cameraOverlayView = self.overlayView;
}

Resources