【问题标题】:setNeedsDisplay on uiimageview doesn't refresh the view immediatelyuiimageview 上的 setNeedsDisplay 不会立即刷新视图
【发布时间】:2014-02-13 20:34:42
【问题描述】:

我正在使用 AVCaptureSession 捕获图像,然后将其发送到我的服务器。

[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 返回了所拍摄图像的镜像(左侧的对象出现在右侧)

【问题讨论】:

    标签: objective-c uiimageview


    【解决方案1】:

    来自文档:

    这种方法使 请求的注释并立即返回。观点不 实际重绘直到下一个绘图周期,此时所有 更新无效的视图。

    由于您在致电 setNeedsDisplay 后正在做其他事情,因此事件不会结束,您也不会立即得到显示。

    如果你想做更多的事情,一种方法是将其余的事情安排在一个新的块中,例如,

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

    【讨论】:

    • 我需要如何更改 addBlockWithOperation 导致我收到错误:选择器“addBlockWithOperation:”没有已知的实例方法?新手遇险:(
    • 您需要将调用-processImage 之后的代码移动到事件的末尾,以便显示有机会运行。我弄错了方法名称,但我的答案现在已更正。你也可以直接用 dispatch_async() 来做。
    猜你喜欢
    • 2013-08-07
    • 2014-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多