iOS 5 - AVCaptureDevice 设置焦点和焦点模式卡住实时相机图片

标签 ios ios5 avcapturedevice avcapture

从 iOS 4 开始,我使用以下方法设置焦点:

- (void) focusAtPoint:(CGPoint)point
{
    AVCaptureDevice *device = [[self captureInput] device];

    NSError *error;

     if ([device isFocusModeSupported:AVCaptureFocusModeAutoFocus] &&
         [device isFocusPointOfInterestSupported])
     {
         if ([device lockForConfiguration:&error]) {
             [device setFocusPointOfInterest:point];
             [device setFocusMode:AVCaptureFocusModeAutoFocus];
             [device unlockForConfiguration];
         } else {
             NSLog(@"Error: %@", error);
         }
     }
}

在 iOS 4 设备上,这没有任何问题。但在 iOS 5 上,实时摄像头画面会卡住,几秒钟后就会完全变黑。没有抛出异常或错误。

如果我注释掉 setFocusPointOfInterest 或 setFocusMode,则不会发生错误。因此,它们两者的结合将导致这种行为。

最佳答案

您为 setFocusPointOfInterest: 函数提供的点不正确。这就是它崩溃的原因。

将此方法添加到您的程序中并使用此函数返回的值

    - (CGPoint)convertToPointOfInterestFromViewCoordinates:(CGPoint)viewCoordinates 
{
    CGPoint pointOfInterest = CGPointMake(.5f, .5f);
    CGSize frameSize = [[self videoPreviewView] frame].size;

    AVCaptureVideoPreviewLayer *videoPreviewLayer = [self prevLayer];

    if ([[self prevLayer] isMirrored]) {
        viewCoordinates.x = frameSize.width - viewCoordinates.x;
    }    

    if ( [[videoPreviewLayer videoGravity] isEqualToString:AVLayerVideoGravityResize] ) {
        pointOfInterest = CGPointMake(viewCoordinates.y / frameSize.height, 1.f - (viewCoordinates.x / frameSize.width));
    } else {
        CGRect cleanAperture;
        for (AVCaptureInputPort *port in [[[[self captureSession] inputs] lastObject] ports]) {
            if ([port mediaType] == AVMediaTypeVideo) {
                cleanAperture = CMVideoFormatDescriptionGetCleanAperture([port formatDescription], YES);
                CGSize apertureSize = cleanAperture.size;
                CGPoint point = viewCoordinates;

                CGFloat apertureRatio = apertureSize.height / apertureSize.width;
                CGFloat viewRatio = frameSize.width / frameSize.height;
                CGFloat xc = .5f;
                CGFloat yc = .5f;

                if ( [[videoPreviewLayer videoGravity] isEqualToString:AVLayerVideoGravityResizeAspect] ) {
                    if (viewRatio > apertureRatio) {
                        CGFloat y2 = frameSize.height;
                        CGFloat x2 = frameSize.height * apertureRatio;
                        CGFloat x1 = frameSize.width;
                        CGFloat blackBar = (x1 - x2) / 2;
                        if (point.x >= blackBar && point.x <= blackBar + x2) {
                            xc = point.y / y2;
                            yc = 1.f - ((point.x - blackBar) / x2);
                        }
                    } else {
                        CGFloat y2 = frameSize.width / apertureRatio;
                        CGFloat y1 = frameSize.height;
                        CGFloat x2 = frameSize.width;
                        CGFloat blackBar = (y1 - y2) / 2;
                        if (point.y >= blackBar && point.y <= blackBar + y2) {
                            xc = ((point.y - blackBar) / y2);
                            yc = 1.f - (point.x / x2);
                        }
                    }
                } else if ([[videoPreviewLayer videoGravity] isEqualToString:AVLayerVideoGravityResizeAspectFill]) {
                    if (viewRatio > apertureRatio) {
                        CGFloat y2 = apertureSize.width * (frameSize.width / apertureSize.height);
                        xc = (point.y + ((y2 - frameSize.height) / 2.f)) / y2;
                        yc = (frameSize.width - point.x) / frameSize.width;
                    } else {
                        CGFloat x2 = apertureSize.height * (frameSize.height / apertureSize.width);
                        yc = 1.f - ((point.x + ((x2 - frameSize.width) / 2)) / x2);
                        xc = point.y / frameSize.height;
                    }

                }

                pointOfInterest = CGPointMake(xc, yc);
                break;
            }
        }
    }

    return pointOfInterest;
}

关于iOS 5 - AVCaptureDevice 设置焦点和焦点模式卡住实时相机图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7919485/

相关文章:

iphone - 有什么方法可以强制 UIKit 在某些设备上使用标准(不是 @2x)图像,即使 @2x 图像可用?

ios - 我应该在 iOS 上将分析数据存储在哪个目录中?

在 Xcode 4.5 中开发的 iOS 应用程序在 iOS 6 上运行良好,但在 iOS 5 上崩溃

jquery - 使用 javascript 关闭 iOS safari 选择器

ios - 为什么 autoFocusRangeRestriction 没有效果?

ios - (AVCaptureDevice) 为什么我无法获取我的相机设备

更改语言环境时,iOS 日期选择器最大/最小日期不起作用?

iphone - UILabel 字体 : bold and italic

ios - 如何在 Objective-C 中生成随机大写字符

swift - macOS 上的 AVCaptureDevice