2014-01-23 88 views
0

我正在使用AVCaptureSession捕獲圖像,然後將其發送到我的服務器。uiimageview上的setNeedsDisplay不會立即刷新視圖

[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) { 

     if (imageSampleBuffer != NULL) { 
      NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; 
      self.videoImageData = [imageData copy]; 
      [self processImage:[UIImage imageWithData:imageData]]; 

      NSString* response=[self uploadImage]; 
      [activityIndicator removeFromSuperview]; 
      self.takePictureBtn.enabled = YES; 
      [self setUserMessage:response]; 
     } 
    }]; 

- (void) processImage:(UIImage *)image 
{ 
... 

    [UIView beginAnimations:@"rotate" context:nil]; 
    [UIView setAnimationDuration:0.5]; 
    int degrees = [self rotationDegrees]; 
    CGFloat radians =degrees * M_PI/180.0; 
    self.captureImageView.transform = CGAffineTransformMakeRotation(radians); 
    [UIView commitAnimations]; 
    [self.captureImageView setNeedsDisplay]; 
} 

- (NSString*)uploadImage將照片發送到服務器

我期待的行爲是processImage來終止後,圖像將其顯示在UIImageView的captureImageView活動指示燈旋轉,而是我得到一個白色只有在服務器發送請求終止後,才能看到指示符旋轉的視圖captureImageView顯示圖像

如何實現我想要的行爲?

我面對的是,我的imageSampleBuffer返回取一個的鏡像的另一個問題(左側對象出現在右邊)

回答

4

從文檔:

This method makes a note of the request and returns immediately. The view is not actually redrawn until the next drawing cycle, at which point all invalidated views are updated.

由於您在致電setNeedsDisplay之後正在做其他事情,事件並未結束,您也不會立即看到顯示。

如果你想要做更多的東西,一種方法是安排的剩下的東西在一個新的模塊,比如,與

[[NSOperationQueue mainQueue] addOperationWithBlock:^{ 
    NSString *const response = [self uploadImage]; 
    [activityIndicator removeFromSuperview]; 
    self.takePictureBtn.enabled = YES; 
    [self setUserMessage:response]; 
}]; 
+0

我需要做什麼來改變addBlockWithOperation與事業我得到錯誤:選擇器'addBlockWithOperation:'沒有已知的實例方法?新手遇到困難:( –

+0

您需要在調用-processImage之後將代碼移動到事件結束處,以便顯示器有機會運行。我得到的方法名稱錯誤,但我的答案現在已得到糾正。直接用dispatch_async()也可以。 –