[英]App crashes after running a method in background thread
I am recoding the main screen in which we can drag images. 我正在重新编码我们可以在其中拖动图像的主屏幕。
I have a setneedsdisplay method which slows down the dragging of objects. 我有一个setneedsdisplay方法,它会减慢对象的拖动速度。
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? 另外,如果还有其他选项可以录制视频而无需一次又一次地调用drawRect:方法?
- (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];
}
Possible cause: 可能的原因:
[self performSelectorInBackground:@selector(setNeedsDisplay) withObject:nil];
You are updating your UI from the background thread . 您正在从后台线程更新UI 。 Never do that,If you need to do something with your UI, do it in the main thread. 永远不要这样做,如果您需要对UI进行操作,请在主线程中执行。
Also I found an issue on your code: 我也在您的代码上发现了一个问题:
You are calling the setNeedsDisplay
from the drawRect:
it'll call the drawRect:
again. 您正在从drawRect:
调用setNeedsDisplay
drawRect:
它将再次调用drawRect:
(If you need to update your view call [self setNeedsDisplay];
and never call drawRect:
directly.) (如果需要更新视图,请调用[self setNeedsDisplay];
不要直接调用drawRect:
)
I guess setNeedsDisplay:
must be called on the main thread then. 我猜setNeedsDisplay:
必须在主线程上调用。 Most UI methods don't work on background threads. 大多数UI方法不适用于后台线程。
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? 并尝试仅设置setNeedsDisplayInRect:
如果只想重绘部分显示内容?
You should never call any method that updates the UI in the background thread. 您永远不应调用任何在后台线程中更新UI的方法。 Always update UI from the main thread. 始终从主线程更新UI。 You don't need that line there if talking about your code as drawrect is called when - setNeedsDisplay method is called. 如果在调用setNeedsDisplay方法时调用drawrect来谈论代码,则不需要在那一行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.