liv a
liv a

Reputation: 3340

setNeedsDisplay on uiimageview doesn't refresh the view immediately

I'm using AVCaptureSession to capture an image and then send it to my server.

[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 send the photo to the server

The behavior I'm expecting is after processImage terminates that the image will be displayed in the UIImageView captureImageView on it the activity indicator rotates but instead i get a white blanc view with the indicator rotating and only after the server post request terminates captureImageView displays the image

How can I achieve my desired behavior?

Another problem I'm facing is that my imageSampleBuffer returns a mirror image of the taken one (object on the left appears on the right)

Upvotes: 0

Views: 1352

Answers (1)

Wil Shipley
Wil Shipley

Reputation: 9533

From the docs:

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.

Since you’re doing other stuff after you call setNeedsDisplay the event doesn’t end and you don’t get a display immediately.

If you want to do more stuff, one approach would be to schedule the rest of the stuff in a new block, like, with

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

Upvotes: 4

Related Questions