UIImageView图层不会多次转换

时间:2013-12-08 22:41:42

标签: ios iphone objective-c opencv uiimageview

我有一个UIImageView,我想在回调中进行转换(具体来说:使用OpenCV processImage)。在回调中,我计算了一个应该转换的角度,并将变换设置为该角度。它只在第一次工作,但不会更新(我正在记录角度并确认它发生了变化)。

但是,如果我将一个按钮附加到相同的代码,我可以多次应用转换,没有任何问题。我也可以混合两个动作(在processImage回调中应用rotate并在按钮点击上应用旋转)但旋转只对按钮点击产生影响。

我是否缺少某种重绘方法告诉UIImageView重绘自己在按钮点击时自动调用?

这个actionRotate工作正常,每次点按按钮都会应用旋转。

- (IBAction)actionRotate:(id)sender {
    if (!self.angle) {
        self.angle = 0.5f;
    } else {
        self.angle += 0.1f;
    }

    CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;
    rotationAndPerspectiveTransform.m34 = 1.0 / -500;
    self.image.layer.transform = CATransform3DRotate(rotationAndPerspectiveTransform, self.angle, 0.0f, 1.0f, 0.0f);
    self.image.layer.zPosition = 1000;

    NSLog(@"actionRotate angle: %f", self.angle);
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // init camera
    self.videoCamera = [[CvVideoCamera alloc] initWithParentView:self.output];
    self.videoCamera.delegate = self;
    self.videoCamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionBack;
    self.videoCamera.defaultAVCaptureSessionPreset = AVCaptureSessionPresetMedium;
    self.videoCamera.defaultAVCaptureVideoOrientation = AVCaptureVideoOrientationPortrait;
    // 1 fps to slow down log output for debugging
    self.videoCamera.defaultFPS = 1;
    self.output.frame = CGRectMake(0, 0, 360, 480);
    [self.videoCamera start];
}

processImage不起作用。角度正确递增,但转换未应用。即使我打电话给[self actionRotate:nil]也绝对确定它是运行相同的代码。

- (void)processImage:(Mat&)image;
{
    if (!self.angle) {
        self.angle = 0.5f;
    } else {
        self.angle += 0.1f;
    }

    CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;
    rotationAndPerspectiveTransform.m34 = 1.0 / -500;
    self.image.layer.transform = CATransform3DRotate(rotationAndPerspectiveTransform, self.angle, 0.0f, 1.0f, 0.0f);
    self.image.layer.zPosition = 1000;

    NSLog(@"processImage angle: %f", self.angle);
}

1 个答案:

答案 0 :(得分:1)

我发现它没有在主队列中运行,因此不得不在processImage

中更改执行转换的代码
dispatch_async(dispatch_get_main_queue(), ^{
    self.deviation.layer.transform = CATransform3DRotate(rotationAndPerspectiveTransform, self.angle, 0.0f, 1.0f, 0.0f);
    self.deviation.layer.zPosition = 1000;
});