Reputation: 881
I am recoding the main screen in which we can drag images.
I have a setneedsdisplay method which slows down the dragging of objects.
But when i call the same method in the background thread the issue is solved but the app crashes.
Also if there is any other option to record the video without calling the drawRect: method again and again?
- (void) drawRect:(CGRect)rect {
NSDate* start = [NSDate date];
CGContextRef context = [self createBitmapContextOfSize:self.frame.size];
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, self.frame.size.height);
CGContextConcatCTM(context, flipVertical);
[self.layer renderInContext:context];
CGImageRef cgImage = CGBitmapContextCreateImage(context);
UIImage* background = [UIImage imageWithCGImage: cgImage];
CGImageRelease(cgImage);
self.currentScreen = background;
if (_recording) {
float millisElapsed = [[NSDate date] timeIntervalSinceDate:startedAt] * 1000.0;
[self writeVideoFrameAtTime:CMTimeMake((int)millisElapsed, 1000)];
}
float processingSeconds = [[NSDate date] timeIntervalSinceDate:start];
delayRemaining = (1.0 / self.frameRate) - processingSeconds;
//CGContextRelease(context);
//redraw at the specified framerate
//[self performSelector:@selector(setNeedsDisplay) withObject:nil afterDelay:delayRemaining > 0.0 ? delayRemaining : 0.01];
[self performSelectorInBackground:@selector(setNeedsDisplay) withObject:nil];
}
Upvotes: 0
Views: 1495
Reputation: 107131
Possible cause:
[self performSelectorInBackground:@selector(setNeedsDisplay) withObject:nil];
You are updating your UI from the background thread. Never do that,If you need to do something with your UI, do it in the main thread.
Also I found an issue on your code:
You are calling the setNeedsDisplay
from the drawRect:
it'll call the drawRect:
again.
(If you need to update your view call [self setNeedsDisplay];
and never call drawRect:
directly.)
Upvotes: 4
Reputation: 1744
You should never call any method that updates the UI in the background thread. Always update UI from the main thread. You don't need that line there if talking about your code as drawrect is called when - setNeedsDisplay method is called.
Upvotes: 1
Reputation: 38475
I guess setNeedsDisplay:
must be called on the main thread then. Most UI methods don't work on background threads.
If you put your render methods into your question we can see if they can be improved at all?
And try only setting setNeedsDisplayInRect:
if you only want to redraw part of the display?
Upvotes: 1