1

我正在使用自定义 CaptureSessionManager 录制一些视频,并根据设备方向旋转预览层(否则,预览层在横向左/右上是颠倒的)。到目前为止,这有效。但...

当我尝试播放视频时,我使用以下代码来确定视频的方向:

AVAssetTrack *videoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
    CGSize size = [videoTrack naturalSize];
    CGAffineTransform txf = [videoTrack preferredTransform];

    if (size.width == txf.tx && size.height == txf.ty)
        return UIInterfaceOrientationLandscapeRight;
    else if (txf.tx == 0 && txf.ty == 0)
        return UIInterfaceOrientationLandscapeLeft;
    else if (txf.tx == 0 && txf.ty == size.width)
        return UIInterfaceOrientationPortraitUpsideDown;
    else
        return UIInterfaceOrientationPortrait;

但结果总是一样的,不管是左还是右。

我使用此代码来获取输出:

AVAssetTrack *videoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
    CGSize size = [videoTrack naturalSize];
    CGAffineTransform txf = [videoTrack preferredTransform];

    NSLog(@"Transformation a %f - b %f - c %f - d %f", txf.a, txf.b, txf.c, txf.d);
    NSLog(@"Transformation tx %f - ty %f", txf.tx, txf.ty);
    NSLog(@"Size width: %f - height: %f", size.width, size.height);

这导致:

Transformation a -1.000000 - b 0.000000 - c 0.000000 - d -1.000000
Transformation tx 1920.000000 - ty 1080.000000
Size width: 1920.000000 - height: 1080.000000

左右方向记录。

有任何想法吗?


这取决于您的测试理念。我个人建议不要从功能测试中调用外部服务,并建议使用下面的模拟方法或其他模拟/存根方法。调用外部服务可能会很慢,受网络不稳定的影响(顺便说一下,您可以模拟超时等),对于具有调用配额的服务会出现问题(可能在这种情况下不适用)等等。因此,对于您的功能测试,您可以模拟该服务,以便它返回预设信息。通过这种方式,您可以检查您是否传递了服务敏感信息,并且正在使用您返回的结果做正确的事情。您还可以轻松测试任何错误情况。例如,使用 Mocha,您可以在功能测试中执行以下操作:

api = mock()
canned_auth_response = {:access_token => 'whatever-you-want', :refresh_token => 'blah', :issued_at => 'sometime', :expires_in => 'some-future-time', :id_token => 'some-id'}
api.expects(:authorize).with(has_entries(:oauth2_callback => 'your-url', :oauth2_verification_code => 'whatever-you-put-in-params-code').returns(canned_auth_response)
AdwordsApi::Api.expects(:new).with('your-config-filename').returns(api)

AdwordsApi::Api.new现在,当您的测试调用将返回罐装数据(当然,将其更改为包含适当的数据)时,您将获得一个模拟的 api 对象auth_response,因此您现在可以调用将使用模拟的控制器回调,然后您可以检查您的回调方法是否符合您的预期。您可以从 mock 中返回错误数据或不完整的数据api,或者让它在现实生活中引发异常。expects还会检查您是否将期望的参数传递给外部服务。

我并不是说这是唯一或最好的方法,或者即使上面的代码在我刚刚输入时可以保证工作,但它应该说明方法!

如果您真的想针对真实服务进行测试,这是一个很好的冒烟测试/健全性检查,您可以使用 vcr gem https://github.com/vcr/vcr来记录/重播 http 响应(我已经从未使用过,但看起来不错),或者您可以添加一个仅在特别请求时运行的测试,或者在构建服务器上自动运行。例如,我从 platformatec 的博客文章中引用以下内容,这表明在 rspec 中您可以执行以下操作:

describe KittenInfo::Client, external: true do
  # …
end

然后,在您的 spec_helper.rb 中,只需设置:

RSpec.configure do |config|
  config.filter_run_excluding external: true
end

现在,默认情况下,运行规范会跳过所有将 :external 设置为 true 的组。每当您调整客户端或构建时,您都可以使用以下命令运行这些特定测试:

$ rspec --tag external

有关此 rspec 方法的完整文章,请参阅http://blog.plataformatec.com.br/2012/10/filtering-examples-in-rspec/ 。

4

1 回答 1

0

好吧,愚蠢,愚蠢的错误……</p>

我忘了在输出文件上设置方向:

self.recordingOrientation -> AVCaptureVideoOrientation当设备旋转或开始录制时存储。

AVCaptureConnection *CaptureConnection = [MovieFileOutput connectionWithMediaType:AVMediaTypeVideo];

if ([CaptureConnection isVideoOrientationSupported])
{
    [CaptureConnection setVideoOrientation:self.recordingOrientation];
}
于 2014-02-23T23:18:52.293 回答