【问题标题】:Image capture is distorted when using it for a CALayer将其用于 CALayer 时,图像捕获失真
【发布时间】:2014-02-20 06:29:43
【问题描述】:

我正在开发一个拍照应用程序。使用以下代码将应用的预览层设置为恰好占据屏幕的一半:

[_previewLayer setFrame:CGRectMake(0, 0, rootLayer.bounds.size.width, rootLayer.bounds.size.height/2)];

这看起来很完美,当用户查看相机的“预览”/他们在拍照时看到的内容时,完全没有失真。

但是,一旦他们实际拍摄了照片,我就创建了一个子图层并将其 frame 属性设置为我的预览图层的属性,并将照片设置为子图层的内容。

这在技术上确实有效。用户拍摄照片后,照片会按原样显示在屏幕的上半部分。

唯一的问题是照片变形了。

它看起来被拉长了,就像我在拍风景照片一样。

非常感谢任何帮助

这是我所有的视图控制器的代码:

#import "MediaCaptureVC.h"

@interface MediaCaptureVC ()

@end

@implementation MediaCaptureVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{

    [super viewDidLoad];
    // Do any additional setup after loading the view.


    AVCaptureSession *session =[[AVCaptureSession alloc]init];


    [session setSessionPreset:AVCaptureSessionPresetPhoto];


    AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];


    NSError *error = [[NSError alloc]init];

    AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&error];


    if([session canAddInput:deviceInput])
        [session addInput:deviceInput];


    _previewLayer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:session];


    [_previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

    CALayer *rootLayer = [[self view]layer];

    [rootLayer setMasksToBounds:YES];


    [_previewLayer setFrame:CGRectMake(0, 0, rootLayer.bounds.size.width, rootLayer.bounds.size.height/2)];


    [rootLayer insertSublayer:_previewLayer atIndex:0];


    _stillImageOutput = [[AVCaptureStillImageOutput alloc] init];

  [session addOutput:_stillImageOutput];

    [session startRunning];


    }

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(UIImage*) rotate:(UIImage*) src andOrientation:(UIImageOrientation)orientation
{
    UIGraphicsBeginImageContext(src.size);

    CGContextRef context=(UIGraphicsGetCurrentContext());

    if (orientation == UIImageOrientationRight) {
        CGContextRotateCTM (context, 90/180*M_PI) ;
    } else if (orientation == UIImageOrientationLeft) {
        CGContextRotateCTM (context, -90/180*M_PI);
    } else if (orientation == UIImageOrientationDown) {
        // NOTHING
    } else if (orientation == UIImageOrientationUp) {
        CGContextRotateCTM (context, 90/180*M_PI);
    }

    [src drawAtPoint:CGPointMake(0, 0)];
    UIImage *img=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;

}



-(IBAction)stillImageCapture {

    AVCaptureConnection *videoConnection = nil;
    for (AVCaptureConnection *connection in _stillImageOutput.connections){
        for (AVCaptureInputPort *port in [connection inputPorts]){

            if ([[port mediaType] isEqual:AVMediaTypeVideo]){

                videoConnection = connection;
                break;
            }
        }
        if (videoConnection) {
            break;
        }
    }

    NSLog(@"about to request a capture from: %@", _stillImageOutput);

[_stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {


if(imageDataSampleBuffer) {

           NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];


            UIImage *image = [[UIImage alloc]initWithData:imageData];


        image = [self rotate:image andOrientation:image.imageOrientation];


            CALayer *subLayer = [CALayer layer];

            CGImageRef imageRef = image.CGImage;

    subLayer.contents = (id)[UIImage imageWithCGImage:imageRef].CGImage;

          subLayer.frame = _previewLayer.frame;

            CALayer *rootLayer = [[self view]layer];

          [rootLayer setMasksToBounds:YES];

            [subLayer setFrame:CGRectMake(0, 0, rootLayer.bounds.size.width, rootLayer.bounds.size.height/2)];

           [_previewLayer addSublayer:subLayer];


            NSLog(@"%@", subLayer.contents);

            NSLog(@"Orientation: %d", image.imageOrientation);

        }

    }];

}

@end

【问题讨论】:

  • 你可以设置你的imageview的内容模式。

标签: ios iphone objective-c avfoundation avcapturesession


【解决方案1】:

您好,希望对您有所帮助 -

代码似乎比应有的复杂,因为大多数代码是在 CALayer 级别而不是 imageView / view 级别完成的,但是我认为问题在于从原始捕获到您的迷你视口的帧比例是不同的,这会扭曲此语句中的 UIImage :

  [subLayer setFrame:CGRectMake(0, 0, rootLayer.bounds.size.width, rootLayer.bounds.size.height/2)];

需要做的是捕获sublayer.frame的比例,并获得适合rootlayer或与之关联的Image view的最佳尺寸

在此之前我有一些代码:在处理比例之前编写了一个子例程(请注意,您需要调整框架的原点以获得您想要的!)

... CGRect newbounds = [self figure_proportion:image to_fit_rect(rootLayer.frame) if (newbounds.size.height

  -(CGRect) figure_proportion:(UIImage *) image2 to_fit_rect:(CGRect) rect  {
     CGSize image_size = image2.size;
      CGRect newrect = rect;
    float wfactor = image_size.width/ image_size.height;
    float hfactor = image_size.height/ image_size.width;

   if (image2.size.width > image2.size.height) {
       newrect.size.width = rect.size.width;
       newrect.size.height = (rect.size.width   * hfactor);
     }
   else if (image2.size.height > image2.size.width) {
       newrect.size.height = rect.size.height;
       newrect.size.width = (rect.size.height   * wfactor);
    }
   else {
      newrect.size.width = rect.size.width;
      newrect.size.height = newrect.size.width;
   }
   if (newrect.size.height > rect.size.height) {
       newrect.size.height = rect.size.height;
       newrect.size.width = (newrect.size.height* wfactor);
    }
   if (newrect.size.width > rect.size.width) {
       newrect.size.width = rect.size.width;
       newrect.size.height = (newrect.size.width* hfactor);
  }
   return(newrect);


   }

【讨论】:

    猜你喜欢
    • 2018-12-03
    • 2010-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-05
    • 1970-01-01
    相关资源
    最近更新 更多