iPad 3 Device Crash with renderInContext

我的未来我决定 提交于 2019-12-20 03:26:24

问题


I have been using the following code in my application to render out a custom UIView to an image. The code works great in the Simulator (iPad and iPad retina) as well as on iPad 1 and iPad 2 devices. However, I recently tested it on the iPad 3 device, and am getting a crash that I can't seem to resolve. The relevant code snippet is:

UIGraphicsBeginImageContext(CGSizeMake(myUIView.frame.size.width, myUIView.frame.size.height));
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
[myUIView.layer renderInContext:context];

The application crashes on the last line. The following is in the crash log:

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x12311000
Crashed Thread:  5

And from later in the log:

Thread 5 name:  Dispatch queue: com.apple.root.default-overcommit-priority
Thread 5 Crashed:
0   ImageIO                         0x3384bcb4 ImageIO_ABGR_TO_ARGB_8Bit + 68
1   ImageIO                         0x3388761c __copyImageBlockSetPNG_block_invoke_1 +  608
2   libdispatch.dylib               0x348c0c52 _dispatch_call_block_and_release + 6
3   libdispatch.dylib               0x348c3810 _dispatch_worker_thread2 + 252
4   libsystem_c.dylib               0x33145df4 _pthread_wqthread + 288
5   libsystem_c.dylib               0x33145cc8 start_wqthread + 0

I first thought that this was a memory management error, but it's only on the iPad 3 device and I'm using ARC. This has me stumped.

Any suggestions of where I might look to troubleshoot this further? Has anyone else encountered this sort of behavior?

Thanks in advance!


回答1:


Try

UIGraphicsBeginImageContextWithOptions(CGSizeMake(myUIView.frame.size.width, myUIView.frame.size.height), NO, [[UIScreen mainScreen] scale]);



回答2:


Was finally able to track down the source of the crash to a particular UIImageView in the view that I was trying to render. This view was using a 2x graphic for iPad 3 retina. While it displayed just fine, it apparently proved to consume too much memory during the renderInContext call. I reduced the size of the graphic and also reduced the rendering scale in the context options. This resolved the crash.



来源:https://stackoverflow.com/questions/11253972/ipad-3-device-crash-with-renderincontext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!