我有一些奇怪的问题,我们的应用,它崩溃时,使用一些UIImage
。我使用[UIImage imageNamed:@"imageName"]
从图像资产中获取图像。但在某些设备上,它会返回nil
,这会使我的应用程序崩溃,但不应该是nil
。我已经检查过了,并且它在主线程上运行,还剩下足够的内存(尽管内存不足)。
图像是PDF格式的单矢量图像在图像资产中,这应该会创建图像的正确大小.
有谁能给我一些如何解决这个问题的建议呢?
Thread : Crashed: com.apple.main-thread
0 CoreFoundation 0x1844d7108 CFDataGetBytePtr + 36
1 Foundation 0x18545a848 bytesInEncoding + 204
2 CoreFoundation 0x1844e88d4 -[__NSCFString UTF8String] + 80
3 CoreUI 0x18d6827c0 -[CUIStructuredThemeStore _canGetRenditionWithKey:isFPO:lookForSubstitutions:] + 780
4 CoreUI 0x18d6a5614 -[CUICatalog _resolvedRenditionKeyFromThemeRef:withBaseKey:scaleFactor:deviceIdiom:deviceSubtype:sizeClassHorizontal:sizeClassVertical:memoryClass:graphicsClass:graphicsFallBackOrder:] + 1484
5 CoreUI 0x18d6a4784 -[CUICatalog namedLookupWithName:scaleFactor:deviceIdiom:deviceSubtype:sizeClassHorizontal:sizeClassVertical:] + 148
6 UIKit 0x18a3df338 __98-[_UIAssetManager imageNamed:scale:idiom:subtype:cachingOptions:sizeClassPair:attachCatalogImage:]_block_invoke + 424
7 UIKit 0x18a3df0d8 -[_UIAssetManager imageNamed:scale:idiom:subtype:cachingOptions:sizeClassPair:attachCatalogImage:] + 212
8 UIKit 0x18a4f1698 -[UIImageAsset imageWithTraitCollection:] + 404
9 UIKit 0x18a3df7c0 -[_UIAssetManager imageNamed:withTrait:] + 276
10 UIKit 0x189e7277c +[UIImage imageNamed:inBundle:compatibleWithTraitCollection:] + 220
11 UIKit 0x189ccb47c +[UIImage imageNamed:] + 124
12 Speakap 0x1000bef50 -[LoadingView commonInit] (LoadingView.m:74)
13 Speakap 0x1000beabc -[LoadingView initWithFrame:] (LoadingView.m:28)
14 Speakap 0x1001348dc -[BaseTableViewController viewDidLoad] (BaseTableViewController.m:32)
15 Speakap 0x1001570d4 -[BaseMessageViewController viewDidLoad] (BaseMessageViewController.m:66)
16 Speakap 0x10014aa34 -[MessageViewController viewDidLoad] (MessageViewController.m:37)
17 UIKit 0x189b8c098 -[UIViewController loadViewIfRequired] + 996
18 UIKit 0x189ba4350 -[UIViewController __viewWillAppear:] + 132
19 UIKit 0x189d3dfb4 -[UINavigationController _startCustomTransition:] + 1052
20 UIKit 0x189c4a190 -[UINavigationController _startDeferredTransitionIfNeeded:] + 688
21 UIKit 0x189c49e6c -[UINavigationController __viewWillLayoutSubviews] + 60
22 UIKit 0x189c49dd4 -[UILayoutContainerView layoutSubviews] + 208
23 UIKit 0x189b877ac -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 644
24 QuartzCore 0x189386b58 -[CALayer layoutSublayers] + 148
25 QuartzCore 0x189381764 CA::Layer::layout_if_needed(CA::Transaction*) + 292
26 QuartzCore 0x189381624 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 32
27 QuartzCore 0x189380cc0 CA::Context::commit_transaction(CA::Transaction*) + 252
28 QuartzCore 0x189380a08 CA::Transaction::commit() + 512
29 UIKit 0x189b7d9d8 _afterCACommitHandler + 180
30 CoreFoundation 0x1845afbd0 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32
31 CoreFoundation 0x1845ad974 __CFRunLoopDoObservers + 372
32 CoreFoundation 0x1845adda4 __CFRunLoopRun + 928
33 CoreFoundation 0x1844dcca0 CFRunLoopRunSpecific + 384
34 GraphicsServices 0x18f718088 GSEventRunModal + 180
35 UIKit 0x189bf4ffc UIApplicationMain + 204
36 Speakap 0x100162b24 main (main.m:14)
37 libdyld.dylib 0x19990a8b8 start + 4
发布于 2015-12-12 08:33:37
在低内存上下文中使用[UIImage imageNamed:@""]
进行内存管理时,我遇到了一些“麻烦”。
如文档所述:ref/occ/clm/UIImage/imageNamed
讨论 此方法在系统缓存中查找具有指定名称的图像对象,如果存在该对象,则返回该对象。如果匹配的图像对象尚未在缓存中,则此方法定位并从磁盘或资产目录加载图像数据,然后返回结果对象。在iOS 9及更高版本中,此方法是线程安全的。
我不知道崩溃发生在哪个操作系统上,但这可能是个主意。
另外一点,如果用imageWithContentOfFile:
或initWithContentOfFile:
替换imageWithContentOfFile:
或initWithContentOfFile:
,是否仍然会发生这种情况?
内存管理不同(没有系统缓存):ref/occ/instm/UIImage/initWithContentsOfFile
讨论 此方法将图像数据加载到内存中,并将其标记为可清除。如果数据被清除并需要重新加载,则图像对象将再次从指定路径加载该数据。
发布于 2015-12-08 06:46:11
https://github.com/rickytan/RTImageAssets
将上面的插件安装到xcode并转到文件-> imageAssets ->Generate缺失的assets.i将生成所有丢失的图像。
发布于 2015-12-07 17:40:50
我在这里看到的唯一可能是,一个特定的设备可能没有正确的文件。例如,如果设备具有Retina显示,并且图像资产中没有@2x图像,则它将以零等形式返回。
我的建议是确保图像资产中确实存在所有所需的大小+确保为imageNamed:参数使用资产组名称。
另外,您是在各种模拟器或实际设备上测试它吗?
https://stackoverflow.com/questions/34022891
复制相似问题