Duck
Duck

Reputation: 35953

Camera Overlay not in the center

I am trying to add an overlay to this UIImagePickerController instance but the result is the overlay off center.

I have created a simple red square (300x300 pt) to exemplify the problem and serve as an overlay and added that to the camera overlay, using this:

  CGRect bounds = [[UIScreen mainScreen] bounds];

  UIView *containerOverlay = [[UIView alloc] initWithFrame:bounds];

  UIImage *overlay = [UIImage imageNamed:@"red"];
  UIImageView *overlayView = [[UIImageView alloc] initWithImage:overlay];
  CGFloat midX = CGRectGetMidX(bounds);
  CGFloat midY = CGRectGetMidY(bounds);

  CGPoint center = CGPointMake(midX, midY);
  overlayView.center = center;
  [containerOverlay addSubview:overlayView];


 [_camera setCameraOverlayView:containerOverlay];

this is what is shown on the live preview... completely off the vertical center.

enter image description here

amazingly, when I take the picture and iOS shows me the picture already taken, the overlay is magically centered...

enter image description here

How do I center the overlay on the live camera?

Upvotes: 0

Views: 1630

Answers (1)

fz.
fz.

Reputation: 3233

Well the overlay is y-centered to the screen bounds right?

enter image description here

Which is what you are doing here:

CGFloat midX = CGRectGetMidX(bounds);
CGFloat midY = CGRectGetMidY(bounds);

If you want the overlay aligned to the actual image rect you need to offset the bottom black bar (it's not the same height as the top black bar).

Upvotes: 1

Related Questions