1

I'm developing an iOS application with latest SDK.

I'm working with camera and I want to make zoom. To do it I have this code:

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self setUpVideo];
}

- (void)setUpVideo
{
    NSLog(@"Set up video");
    UIView *view = [self videoPreviewView];
    CALayer *viewLayer = [view layer];

    [viewLayer setMasksToBounds:YES];

    AVCaptureVideoPreviewLayer *newCaptureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:DataExchanger.cameraManager.captureSession];
    CGRect bounds = [view bounds];

    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    CGAffineTransform t;

    if (orientation == UIInterfaceOrientationLandscapeRight)
    {
        t = CGAffineTransformMakeRotation(-M_PI_2);
        bounds = CGRectMake(bounds.origin.x, bounds.origin.y,
                                bounds.size.height, bounds.size.width);
    }

    [newCaptureVideoPreviewLayer setFrame:bounds];
    [newCaptureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

    newCaptureVideoPreviewLayer.position = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
    newCaptureVideoPreviewLayer.affineTransform = t;

    [viewLayer insertSublayer:newCaptureVideoPreviewLayer below:[[viewLayer sublayers] objectAtIndex:0]];

    CGAffineTransform currentTransform = CGAffineTransformIdentity;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, 2.0f, 2.0f);
    view.transform = newTransform;
}

But I see a different between the zoom image and the same image without zoom. Without moving the camera I see that the center shifted to the left bottom when I made zoom.

Maybe I have to move videoView centers with a formula that calculates its new center using the scale.

Do you know how to fix that?

VansFannel
  • 45,055
  • 107
  • 359
  • 626
  • 1
    I believe that you can change the origin point for the view from the default of the bottom left to the centre so you avoid this affect. I'm typing this off the top of my head, haven;t researched yet else I'd post it as an answer. Look for something like CGAffineTransformCTM. – Cocoadelica Mar 19 '13 at 09:33
  • The left bottom corner is not the scale anchor point, so is normal to see that iv moves when scaled. – Ramy Al Zuhouri Mar 19 '13 at 09:48
  • 1
    This link should answer your question: http://stackoverflow.com/questions/8757613/scale-with-cgaffinetransform-and-set-the-anchor – Johann Dirdal Mar 19 '13 at 09:49
  • @JohannDirdal I've tested everything on that question and it doesn't work for me. – VansFannel Mar 19 '13 at 10:30
  • I'm going crazy with this issue. Do you know how to fix it? – VansFannel Mar 19 '13 at 11:45
  • After scaling the image, have you tried setting the anchor point of the view? myView.layer.anchorPoint = CGPointMake(0, 0); // 0,0 being top left on the iPhone (The point chosen is random, you have to set the anchor point yourself). – Johann Dirdal Mar 19 '13 at 13:23
  • @JohannDirdal No, it doesn't work. If I do that I get a black screen. – VansFannel Mar 19 '13 at 13:57
  • I have updated my question with all the code that I'm using now. – VansFannel Mar 19 '13 at 13:58
  • I think the problem is related with orientation: I'm only using Landscape right orientation, but on `setUpVideo` I get portrait orientation frame: {{0, 0}, {320, 568}} (the landscape frame is {{0, 0}, {568, 320}}. – VansFannel Mar 19 '13 at 14:47

0 Answers0