iOS - [UIImage initWithCGImage:scale:orientation:] 在后台线程上崩溃

Posted

技术标签:

【中文标题】iOS - [UIImage initWithCGImage:scale:orientation:] 在后台线程上崩溃【英文标题】:iOS -[UIImage initWithCGImage:scale:orientation:] crashes on a background thread 【发布时间】:2016-05-07 00:54:51 【问题描述】:

我正在使用-[UIImage initWithCGImage:scale:orientation:] 在后台队列上创建一个UIImage,因此它崩溃了:

崩溃 1

Crashed: com.apple.root.background-qos
0  libsystem_kernel.dylib         0x183778140 __pthread_kill + 8
1  libsystem_pthread.dylib        0x183840ef8 pthread_kill + 112
2  libsystem_c.dylib              0x1836e9dac abort + 140
3  libsystem_malloc.dylib         0x1837acd34 free_list_checksum_botch + 438
4  libsystem_malloc.dylib         0x1837aced8 free_tiny_botch + 84
5  CoreFoundation                 0x183c08038 __CFBasicHashRehash + 2448
6  CoreFoundation                 0x183c09034 __CFBasicHashAddValue + 100
7  CoreFoundation                 0x183ab49c4 CFDictionarySetValue + 248
8  UIKit                          0x18926fd30 _UITraitCollectionCacheForBuiltinStorage + 152
9  UIKit                          0x189272090 +[UITraitCollection traitCollectionWithDisplayScale:] + 52
10 UIKit                          0x1888d5070 -[UIImage initWithCGImage:scale:orientation:] + 236
11 UIKit                          0x1888d4f74 +[UIImage imageWithCGImage:scale:orientation:] + 72
12 MyApp                          0x10001a168 -[UIImage(Additions) foo_scaledImageData] (Foo.m:1527)
13 MyApp                          0x100017304 -[Foo bar] (Foo.m:731)
14 MyApp                          0x100017094 __95-[Foo bar]_block_invoke_3 (Foo.m:691)
15 libdispatch.dylib              0x183629630 _dispatch_call_block_and_release + 24
16 libdispatch.dylib              0x1836295f0 _dispatch_client_callout + 16
17 libdispatch.dylib              0x183637a88 _dispatch_root_queue_drain + 2140
18 libdispatch.dylib              0x183637224 _dispatch_worker_thread3 + 112
19 libsystem_pthread.dylib        0x18383d470 _pthread_wqthread + 1092
20 libsystem_pthread.dylib        0x18383d020 start_wqthread + 4

我也遇到了以下两个线程的崩溃:

崩溃 2,线程 1:

Crashed: com.apple.root.background-qos
0  libsystem_kernel.dylib         0x237d3c5c __pthread_kill + 8
1  libsystem_pthread.dylib        0x23879b47 pthread_kill + 62
2  libsystem_c.dylib              0x237680c5 abort + 108
3  libsystem_malloc.dylib         0x238040e9 free_list_checksum_botch + 362
4  libsystem_malloc.dylib         0x23804105 free_list_checksum_botch + 28
5  libsystem_malloc.dylib         0x237fbbff tiny_malloc_from_free_list + 202
6  libsystem_malloc.dylib         0x237fa987 szone_malloc_should_clear + 218
7  libsystem_malloc.dylib         0x237fa879 malloc_zone_malloc + 88
8  CoreFoundation                 0x23a4e291 _CFRuntimeCreateInstance + 236
9  CoreGraphics                   0x24e792a9 CGTypeCreateInstance + 20
10 CoreGraphics                   0x24db7d23 CGColorTransformCreate + 246
11 ImageIO                        0x2533c64b IIO_ConvertCGColorToColorComponents + 26
12 ImageIO                        0x252f5775 CGImagePixelDataProviderCreate + 276
13 ImageIO                        0x25319af1 CGImagePixelDataProviderCreateConforming + 1684
14 ImageIO                        0x252f418d CGImageDestinationAddImage + 2852
15 UIKit                          0x283fe4f5 _UIImageJPEGRepresentation + 620
16 MyApp                          0xf6785 -[UIImage(Additions) foo_scaledImageData] (Foo.m:1538)
17 MyApp                          0xf3ecd -[Foo bar] (Foo.m:731)
18 MyApp                          0xf3d29 __95-[Foo bar]_block_invoke_3 (Foo.m:691)
19 libdispatch.dylib              0x236d7cbf _dispatch_call_block_and_release + 10
20 libdispatch.dylib              0x236e36a1 _dispatch_root_queue_drain + 1572
21 libdispatch.dylib              0x236e307b _dispatch_worker_thread3 + 94
22 libsystem_pthread.dylib        0x23876e0d _pthread_wqthread + 1024
23 libsystem_pthread.dylib        0x238769fc start_wqthread + 8

崩溃 2,线程 2:

com.apple.root.background-qos
0  libsystem_malloc.dylib         0x238041d8 free_tiny_botch
1  CoreFoundation                 0x23b6f560 __CFBasicHashRehash + 2968
2  CoreFoundation                 0x23b706d4 __CFBasicHashAddValue + 100
3  CoreFoundation                 0x23a4f933 CFDictionarySetValue + 206
4  UIKit                          0x28a87ffb _UITraitCollectionCacheForBuiltinStorage + 146
5  UIKit                          0x28a8a2a7 +[UITraitCollection traitCollectionWithDisplayScale:] + 50
6  UIKit                          0x2812390f -[UIImage initWithCGImage:scale:orientation:] + 214
7  UIKit                          0x2812382b +[UIImage imageWithCGImage:scale:orientation:] + 62
8  MyApp                          0xf676d -[UIImage(Additions) foo_scaledImageData] (Foo.m:1527)
9  MyApp                          0xf3ecd -[Foo bar] (Foo.m:731)
10 MyApp                          0xf3d29 __95-[Foo bar]_block_invoke_3 (Foo.m:691)
11 libdispatch.dylib              0x236d7cbf _dispatch_call_block_and_release + 10
12 libdispatch.dylib              0x236e36a1 _dispatch_root_queue_drain + 1572
13 libdispatch.dylib              0x236e307b _dispatch_worker_thread3 + 94
14 libsystem_pthread.dylib        0x23876e0d _pthread_wqthread + 1024
15 libsystem_pthread.dylib        0x238769fc start_wqthread + 8

崩溃 2,线程 2 看起来像崩溃 1,线程 1,这让我认为它们是相关的,但崩溃 2,线程 2 是异常值。我有很多其他看起来像没有线程 2 堆栈的 Crash 2 的崩溃,这让我认为 UIImageJPEGRepresentation 也可能不是线程安全的。

【问题讨论】:

【参考方案1】:

这看起来类似于AFNetworking issue

我无法说出来源,但正在处理中。 (或至少一个 雷达已被 Apple 内部人员提交)

旁注:我查看了 UIKit 反汇编和 API 调用 traitCollection 和其他一些东西,真的是一切,但 线程安全;更多所以它真的应该只在 main 上调用 理想的线程。我们可以调配并将锁直接添加到 实施,这至少会消除任何事实 非 AFNetworking 消费者仍然会在没有锁的情况下调用它,并且 可能触发竞态条件;但它也很不漂亮。

直接使用 CGImageSource 工作量更大,但可能更好 解决方案。

正如AFNetworking 问题所提到的和堆栈跟踪所显示的,这看起来像是-[UIImage initWithCGImage:scale:orientation:] 调用到UITraitCollection。我在UITraitCollection.hUITraitCollection documentation 中找不到任何说UITraitCollection 是线程安全的。

因此,即使UIImage documentation 说UIImages 是线程安全的:

因为图像对象是不可变的,你不能改变它们 创建后的属性。大多数图像属性是自动设置的 使用随附图像文件或图像数据中的元数据。这 图像对象的不可变性质也意味着它们可以安全使用 来自任何线程。

文档必须表明它们在主线程上创建后可以安全使用,或者如果它们只是按顺序创建,它们可能可以安全使用。 (这就是AFNetworking fixed their issue。)对于我的解决方法,我改用CoreGraphics

CGImageRef image = ...
CGSize scaledSize = ...

CGContextRef context = CGBitmapContextCreate(NULL,
                                             scaledSize.width,
                                             scaledSize.height,
                                             CGImageGetBitsPerComponent(image),
                                             CGImageGetBytesPerRow(image),
                                             CGImageGetColorSpace(image),
                                             CGImageGetAlphaInfo(image));

CGImageRef scaledImage;
if (context)

    CGContextDrawImage(context,
                       CGRectMake(0,
                                  0,
                                  scaledSize.width,
                                  scaledSize.height),
                       image);
    scaledImage = CGBitmapContextCreateImage(context);
    CGContextRelease(context);

else

    scaledImage = NULL;

然后,为了避免在后台线程上使用UIImageJPEGRepresentation,我使用ImageIO 来导出JPEG 数据:

NSData *NSDataFromCGImage(CGImageRef scaledImage) 

    NSMutableData *scaledImageMutableData = scaledImage ? [NSMutableData new] : nil;
    CGDataConsumerRef scaledImageDataConsumer = scaledImageMutableData ? CGDataConsumerCreateWithCFData((__bridge CFMutableDataRef) scaledImageMutableData) : NULL;
    // from the ios 9.3 header:
    // The `options' dictionary is reserved for future use; currently, you
    // should pass NULL for this parameter.
    CGImageDestinationRef scaledImageDestination = scaledImageDataConsumer ? CGImageDestinationCreateWithDataConsumer(scaledImageDataConsumer,
                                                                                                                      kUTTypeJPEG,
                                                                                                                      1,
                                                                                                                      NULL) : NULL;
    BOOL success = (^BOOL ()
                    
                        if (scaledImageDestination)
                        
                            NSDictionary *addImageOptions = @(__bridge NSString *)kCGImageDestinationLossyCompressionQuality : @(0.9),
                                                              ;
                            CGImageDestinationAddImage(scaledImageDestination,
                                                       scaledImage,
                                                       (__bridge CFDictionaryRef)addImageOptions);

                            return (BOOL) CGImageDestinationFinalize(scaledImageDestination);
                        
                        else
                        
                            return NO;
                        
                    )();
    if (scaledImageDestination)
    
        CFRelease(scaledImageDestination);
    
    CGDataConsumerRelease(scaledImageDataConsumer);

    if (success)
    
        return scaledImageMutableData;
    
    else
    
        return nil;
    

【讨论】:

以上是关于iOS - [UIImage initWithCGImage:scale:orientation:] 在后台线程上崩溃的主要内容,如果未能解决你的问题,请参考以下文章

iOS 导航栏黑线,UIImage 枚举处理方式

iOS_UIImage_图片剪切

iOS NSMutableDictionary中UIImage的存储和读取

iOS - 将 UIImage 转换为 vImage 内存处理

如何在 iOS 上删除 UIImage 中的矩形?

IOS将UIView转化为UIImage