0

我有一个使用相机拍照的 iOS 应用程序。它使用CGPath在屏幕上绘制的路径()(例如矩形),并在该路径内拍摄照片。该应用程序仅支持纵向。

为此,我使用: AVCaptureSession, AVCaptureStillImageOutput, AVCaptureDevice, AVCaptureVideoPreviewLayer (我想开发这种应用程序的开发人员都很熟悉)。

我的代码使用UIScreen.mainScreen().boundsUIScreen.mainScreen().scale适应各种设备并完成其工作。

一切都很好(在 iPhone 5、iPhone 6 上),直到我在 iPhone 6+(运行 iOS 9.3.1)上尝试该应用程序并发现有问题。拍摄的照片不再放置在正确的位置。

我让某人试用了 iPhone 6+,并通过发送适当的消息来确认 ( UIScreen.mainScreen().scale) 应该是:3.0。我在项目中放置了适当尺寸的启动图像(640 × 960、640 × 1136、750 × 1334、1242 × 2208)。那么可能是什么问题呢?

4

2 回答 2

0

我在应用程序中使用下面的代码,它适用于 6+。

代码启动一个 AVCaptureSession,从设备的摄像头中提取视频输入。

当它这样做时,它会不断更新来自 captureOutput 委托函数的 runImage 变量。

当用户想要拍照时,会调用 takePhoto 方法。此方法创建一个临时 UIImageview 并将 runImage 输入其中。然后使用这个临时 UIImageView 将另一个名为 currentImage 的变量绘制到设备的比例。

在我的例子中,currentImage 是方形的,与 previewHolder 框架相匹配,但我想你可以制作任何你想要的东西。

声明这些:

AVCaptureDevice * device;
AVCaptureDeviceInput * input;
AVCaptureVideoDataOutput * output;
AVCaptureSession * session;
AVCaptureVideoPreviewLayer * preview;
AVCaptureConnection * connection;
UIImage * runImage;

加载扫描仪:

-(void)loadScanner 
{

    device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
    output = [AVCaptureVideoDataOutput new];
    session = [AVCaptureSession new];

    [session setSessionPreset:AVCaptureSessionPresetPhoto];
    [session addInput:input];
    [session addOutput:output];

    [output setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
    [output setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];

    preview = [AVCaptureVideoPreviewLayer layerWithSession:session];
    preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
    preview.frame = previewHolder.bounds;

    connection = preview.connection;
    [connection setVideoOrientation:AVCaptureVideoOrientationPortrait];
    [previewHolder.layer insertSublayer:preview atIndex:0];

}

正在进行的图像捕获,更新 runImage var。

-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection 
{

    runImage = [self imageForBuffer:sampleBuffer];

}

与上述有关。

-(UIImage *)imageForBuffer:(CMSampleBufferRef)sampleBuffer 
{

    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    CVPixelBufferLockBaseAddress(imageBuffer, 0);
    void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
    CGImageRef quartzImage = CGBitmapContextCreateImage(context);
    CVPixelBufferUnlockBaseAddress(imageBuffer,0);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    UIImage *image = [UIImage imageWithCGImage:quartzImage];
    CGImageRelease(quartzImage);

    UIImage * rotated = [[UIImage alloc] initWithCGImage:image.CGImage scale:1.0 orientation:UIImageOrientationRight];

    return rotated;
}

拍照时:

-(void)takePhoto 
{

    UIImageView * temp = [UIImageView new];
    temp.frame = previewHolder.frame;
    temp.image = runImage;
    temp.contentMode = UIViewContentModeScaleAspectFill;
    temp.clipsToBounds = true;
    [self.view addSubview:temp];

    UIGraphicsBeginImageContextWithOptions(temp.bounds.size, NO, [UIScreen mainScreen].scale);
    [temp drawViewHierarchyInRect:temp.bounds afterScreenUpdates:YES];
    currentImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [temp removeFromSuperview];

    //further code...

}
于 2016-04-07T09:20:46.367 回答
-1

以防其他人有同样的问题。这就是让我出错的原因:

我正在命名一个文件:xyz@2x.png。

当 UIScreen.mainScreen().scale == 3.0(iPhone 6+ 的情况)时,它必须命名为:xyz@3x.png。

于 2016-04-14T11:12:17.353 回答