关于uiimage imagenamed内存可能占用大的替代方法

iOS开发中UIImage使用技巧
本文讨论了在iOS开发中使用UIImage的方法,并强调了对于大图应避免使用UIImage imageNamed:方法以减少内存占用。文中提供了替代方案并解释了高清屏下图片适配的问题。
刚才和群里人讨论,确实需要慎用  

[UIImage imageNamed:@"GcBg.png"]   这个方法,因为它可能有缓存,会占用较大的内存。所以小图的话还行,大图的话就别用这个方法了,群里兄弟推荐大图用这个方法:

[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"xxx", imageIndex] ofType:@"png"]];



再啰嗦一句:

还在看敢吃的项目,项目里的代码都是这一句:

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"GcBg.png"]];

但我在工程中并未找到 GcBg.png  这张图片,只有一个带后缀的 GcBg@2x.png ,群里兄弟的解释是:

代码里不需要加后缀,只需要在工程中加上后缀,然后如果是高清屏的话它就会自动去匹配 @2x 这种图片,如果你工程中只有 @2x 图片,而代码是用上种写法的话那么普通屏是无法加载到图片的(所以一般推荐工程中要弄三张图:普通的,@2x,-568的

iOS崩溃日志: Application Specific Information: Could not allocate memory. Thread 0 name: Dispatch queue: fmdb.<FMDatabaseQueue: 0x281b26580> Thread 0 Crashed: 0 libswiftCore.dylib 0x1d54294fc swift_slowAlloc.cold.1 + 16 1 libswiftCore.dylib 0x1d53912e0 swift_slowAlloc + 116 2 libswiftCore.dylib 0x1d5391464 swift_allocObject + 52 3 libswiftCore.dylib 0x1d53426c8 specialized static _DictionaryStorage.resize(original:capacity:move:) + 260 4 IPrinter 0x10304597c specialized _NativeDictionary._copyOrMoveAndResize(capacity:moveElements:) + 92 5 IPrinter 0x1031f3e24 closure #1 in DatabaseManager.query(table:condition:sort:) + 1864 6 IPrinter 0x1031f5b0c partial apply for closure #1 in DatabaseManager.query(table:condition:sort:) + 44 7 IPrinter 0x102dd87fc MoyaProvider.endpoint(_:) + 20 8 IPrinter 0x1031f1a7c thunk for @escaping @callee_guaranteed (@guaranteed FMDatabase, @unowned UnsafeMutablePointer<ObjCBool>) -> () + 44 9 IPrinter 0x10347569c __46-[FMDatabaseQueue beginTransaction:withBlock:]_block_invoke + 180 10 libdispatch.dylib 0x1e1f78780 _dispatch_client_callout + 16 11 libdispatch.dylib 0x1e1f5aa50 _dispatch_lane_barrier_sync_invoke_and_complete + 52 12 IPrinter 0x1034755c8 -[FMDatabaseQueue beginTransaction:withBlock:] + 112 13 IPrinter 0x1031f349c DatabaseManager.query(table:condition:sort:) + 384 14 IPrinter 0x103083a98 DatabaseManager.querySystemTemplateData(deviceType:lang:) + 352 15 IPrinter 0x103083b24 @objc DatabaseManager.querySystemTemplateData(deviceType:lang:) + 48 16 IPrinter 0x1028b2e18 +[SFPublicMethods getLocalSystemTemplateData] + 348 17 IPrinter 0x102d16b20 -[FHYAPP refreshLocalTemplateAndMaterialData] + 44 18 IPrinter 0x102c31e00 -[SFG10TabBarController updateStyleTypeNotification] + 52 19 CoreFoundation 0x1db1fc7d8 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 140 20 CoreFoundation 0x1db2990c0 ___CFXRegistrationPost_block_invoke + 84 21 CoreFoundation 0x1db27d8a8 _CFXRegistrationPost + 404 22 CoreFoundation 0x1db20feec _CFXNotificationPost + 680 23 Foundation 0x1d55a9fd4 -[NSNotificationCenter postNotificationName:object:userInfo:] + 88 24 IPrinter 0x102a12940 -[SFLPSettingViewController selectCell:] + 3616 25 UIKitCore 0x1ddf13a84 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:isCellMultiSelect:deselectPrevious:performCustomSelectionAction:] + 1228 帮我分析一下错误原因
最新发布
11-29
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值