UIImage在后台线程中使用CIContext进行模糊 - 使用EXC_BAD_ACCESS

时间:2016-03-21 11:03:11

标签: ios objective-c multithreading uiimage core-image

我试图找到一种在后台线程上模糊UIImage的方法。我尝试了多种方式,最后一次问到这里,我得到了很好的建议,使用 kCIContextUseSoftwareRenderer 。我很确定这将是崩溃的结束,因为主要问题是当应用程序放在后台时GPU正在尝试绘制。这是不允许的,导致崩溃。

这个特定的崩溃现在已经消失,但不幸的是,下面的代码对于我们的许多用户来说仍然崩溃。我们也无法在任何设备上重现崩溃,我们从Fabric(前Crashlytics)获取崩溃日志。

我已经标记了我们接收崩溃报告的两行,都报告了EXC_BAD_ACCESS。

- (UIImage *)blurred:(float)inputRadius
{
// line of crash 1 
CIContext *context = [CIContext contextWithOptions: @{kCIContextUseSoftwareRenderer: @YES}];
CIImage *inputImage = [CIImage imageWithCGImage:self.CGImage];

CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
[filter setValue:inputImage forKey:kCIInputImageKey];
[filter setValue:[NSNumber numberWithFloat:inputRadius] forKey:@"inputRadius"];
CIImage *result = [filter valueForKey:kCIOutputImageKey];

// line of crash 2
CGImageRef cgImage = [context createCGImage:result fromRect:[inputImage extent]];

UIImage *blurredImage = [UIImage imageWithCGImage:cgImage];

CFRelease(cgImage);
return blurredImage;
}

以下是来自用户的示例Stacktrace:

Thread : Crashed: com.apple.root.background-qos
0  libsystem_kernel.dylib         0x193933270 __pthread_kill + 8
1  libsystem_pthread.dylib        0x1939d1170 pthread_kill + 112
2  libsystem_c.dylib              0x1938aab18 abort + 112
3  libsystem_malloc.dylib         0x19396e3e4 _nano_malloc_check_clear + 318
4  libsystem_malloc.dylib         0x19396d2f8 nano_free + 192
5  CoreFoundation                 0x18215e1d4 __CFDataDeallocate + 44
6  CoreFoundation                 0x1820a52b4 CFRelease + 328
7  OpenCL                         0x18d050eb8 clCreateKernelFromDAGAPPLE + 1328
8  CoreImage                      0x182837364 CI::CLDAGHelper::build_complete_kernel() + 36
9  CoreImage                      0x182813d20 CI::CLContext::render_sub_tree(CI::Node*, CI::Node*, int, CI::DestParams*) + 568
10 CoreImage                      0x1828148c0 CI::CLContext::render(CI::Node*) + 180
11 CoreImage                      0x1828247e0 CI::image_get_cgimage(CI::Context*, CI::Image*, CGRect, CGColorSpace*, CI::PixelFormat) + 1552
12 CoreImage                      0x1827ff384 -[CIContext createCGImage:fromRect:format:colorSpace:] + 648
13 Knuddels                       0x10003a768 -[UIImage(blur) blurred:] (UIImage+blur.m:35)
14 Knuddels                       0x100214094 __57-[ConversationMessagesViewController setBackgroundPhoto:]_block_invoke (ConversationMessagesViewController.m:571)
15 libdispatch.dylib              0x1937ed994 _dispatch_call_block_and_release + 24
16 libdispatch.dylib              0x1937ed954 _dispatch_client_callout + 16
17 libdispatch.dylib              0x1937fa780 _dispatch_root_queue_drain + 1848
18 libdispatch.dylib              0x1937fbc4c _dispatch_worker_thread3 + 108
19 libsystem_pthread.dylib        0x1939cd22c _pthread_wqthread + 816
20 libsystem_pthread.dylib        0x1939ccef0 start_wqthread + 4

0 个答案:

没有答案