2013-12-08 58 views
0

我有一個UIImageView,我想在一個回調(特別是:使用OpenCV processImage)內進行轉換。在回調內部,我計算一個角度,在這個角度上它應該被轉換並將轉換設置爲該角度。它僅僅是第一次工作,但不會更新(我正在記錄角度並驗證了它的變化)。UIImageView圖層不會轉換多次

但是,如果我將一個按鈕附加到相同的代碼,我可以多次應用轉換而不會出現任何問題。我也可以混合這兩個動作(在processImage回調中應用旋轉,並在按鈕水龍頭上應用旋轉),但旋轉僅影響按鈕水龍頭。

是否有某種重繪方法,我錯過了告訴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

我發現,它並沒有在主隊列中運行,因此不得不改變代碼在做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; 
}); 
+0

變換我只是有這個問題,感謝您的解決方案:) – Endanke