iOS 5 – AVCaptureDevice设置焦点和对焦模式冻结实时相机图片

iOS 5 – AVCaptureDevice设置焦点和对焦模式冻结实时相机图片,第1张

概述我正在使用以下方法设置自iOS 4以来的焦点: - (void) focusAtPoint:(CGPoint)point{ AVCaptureDevice *device = [[self captureInput] device]; NSError *error; if ([device isFocusModeSupported:AVCaptureFocusMod 我正在使用以下方法设置自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设置焦点和对焦模式冻结实时相机图片全部内容,希望文章能够帮你解决iOS 5 – AVCaptureDevice设置焦点和对焦模式冻结实时相机图片所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1078190.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存