gpt4 book ai didi

ios - React Native 在 iOS 应用程序进入后台时将新的图像副本加载到内存中

转载 作者:行者123 更新时间:2023-11-29 05:22:51 45 4
gpt4 key购买 nike

我创建了一个新的 React Native 项目 (v0.60.4),并在真正的 iPhone 11 Pro iOS 13.1.3 上以 Release模式运行它。 app.js 非常简单:

import React from 'react';
import {
SafeAreaView,
Image,
} from 'react-native';

const App: () => React$Node = () => {
return (
<SafeAreaView>
<Image source={require('./test.png')} />
</SafeAreaView>
);
};

export default App;

我注意到,每次我的应用程序进入后台(当我切换到另一个应用程序时),我都会看到内存使用量激增,而且从未释放过(即使有内存警告)。

enter image description here

我可以看到增加是由应用程序移入后台时创建的新 ImageIO_PNG_Data 引起的。

虽然此图像的大小相对适中,但在我的实际应用程序中,它会在用户应用程序频繁切换后导致应用程序崩溃。

这是用 Swift 编写的同一个应用程序、同一个图像,使用相同的方法创建 RN 正在使用的 UIImage。

enter image description here

没有额外的图像加载到内存中。 RN 做了什么不同的事情,将这些额外的图像加载到内存中,而不是清除旧的图像?当应用程序进入后台时会发生什么?

更新

标记一代后,我注意到所有大图像都来自看起来像 iOS 应用程序进入后台时的快照:

   0 libsystem_kernel.dylib mmap
1 ImageIO _ImageIO_Malloc
2 ImageIO AppleJPEGReadPlugin::copyImageBlockSet(InfoRec*, CGImageProvider*, CGRect, CGSize, __CFDictionary const*)
3 ImageIO IIO_Reader::CopyImageBlockSetProc(void*, CGImageProvider*, CGRect, CGSize, __CFDictionary const*)
4 ImageIO IIOImageProviderInfo::CopyImageBlockSetWithOptions(void*, CGImageProvider*, CGRect, CGSize, __CFDictionary const*)
5 CoreGraphics CGImageProviderCopyImageBlockSetWithOptions
6 QuartzCore CA::Render::copy_image(CGImage*, CGColorSpace*, unsigned int, double, double)
7 QuartzCore CA::Render::prepare_image(CGImage*, CGColorSpace*, unsigned int, double)
8 QuartzCore CA::Layer::prepare_commit(CA::Transaction*)
9 QuartzCore CA::Context::commit_transaction(CA::Transaction*, double)
10 QuartzCore CA::Transaction::commit()
11 UIKitCore __83-[UIApplication _createSnapshotContextForScene:withName:performLayoutWithSettings:]_block_invoke_4
12 UIKitCore -[UIApplication _performWithUICACommitStateSnapshotting:]
13 UIKitCore __83-[UIApplication _createSnapshotContextForScene:withName:performLayoutWithSettings:]_block_invoke_2
14 UIKitCore +[UIView(Animation) performWithoutAnimation:]
15 UIKitCore __83-[UIApplication _createSnapshotContextForScene:withName:performLayoutWithSettings:]_block_invoke
16 UIKitCore -[UIScene _applyOverrideSettings:forActions:]
17 UIKitCore -[UIWindowScene _applySnapshotSettings:forActions:]
18 UIKitCore -[UIApplication _createSnapshotContextForScene:withName:performLayoutWithSettings:]
19 UIKitCore __65-[UIApplication _performSnapshotsWithAction:forScene:completion:]_block_invoke_3
20 FrontBoardServices -[FBSSceneSnapshotAction _executeNextRequest]
21 FrontBoardServices -[FBSSceneSnapshotAction _executeNextRequest]
22 FrontBoardServices -[FBSSceneSnapshotAction executeRequestsWithHandler:completionHandler:expirationHandler:]
23 UIKitCore __65-[UIApplication _performSnapshotsWithAction:forScene:completion:]_block_invoke_2
24 UIKitCore -[UIApplication _beginSnapshotSessionForScene:withSnapshotBlock:]
25 UIKitCore __65-[UIApplication _performSnapshotsWithAction:forScene:completion:]_block_invoke
26 UIKitCore -[UIScene _enableOverrideSettingsForActions:]
27 UIKitCore -[UIScene _performSystemSnapshotWithActions:]
28 UIKitCore -[UIApplication _performSnapshotsWithAction:forScene:completion:]
29 UIKitCore __98-[_UISceneSnapshotBSActionsHandler _respondToActions:forFBSScene:inUIScene:fromTransitionContext:]_block_invoke_3
30 UIKitCore __98-[_UISceneSnapshotBSActionsHandler _respondToActions:forFBSScene:inUIScene:fromTransitionContext:]_block_invoke.30
31 UIKitCore -[UIApplication prepareSnapshotsWithAction:forScene:completion:]
32 UIKitCore __98-[_UISceneSnapshotBSActionsHandler _respondToActions:forFBSScene:inUIScene:fromTransitionContext:]_block_invoke_2
33 UIKitCore -[UIScene _emitSceneSettingsUpdateResponseForCompletion:afterSceneUpdateWork:]
34 UIKitCore -[UIScene scene:didUpdateWithDiff:transitionContext:completion:]
35 UIKitCore -[UIApplicationSceneClientAgent scene:handleEvent:withCompletion:]
36 FrontBoardServices -[FBSSceneImpl updater:didUpdateSettings:withDiff:transitionContext:completion:]
37 FrontBoardServices __88-[FBSWorkspaceScenesClient sceneID:updateWithSettingsDiff:transitionContext:completion:]_block_invoke_2
38 FrontBoardServices -[FBSWorkspace _calloutQueue_executeCalloutFromSource:withBlock:]
39 FrontBoardServices __88-[FBSWorkspaceScenesClient sceneID:updateWithSettingsDiff:transitionContext:completion:]_block_invoke
40 libdispatch.dylib 0x102a9b2a7
41 libdispatch.dylib 0x102a9e9cf
42 FrontBoardServices __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__
43 FrontBoardServices -[FBSSerialQueue _queue_performNextIfPossible]
44 FrontBoardServices -[FBSSerialQueue _performNextFromRunLoopSource]
45 CoreFoundation __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__
46 CoreFoundation __CFRunLoopDoSource0
47 CoreFoundation __CFRunLoopDoSources0
48 CoreFoundation __CFRunLoopRun
49 CoreFoundation CFRunLoopRunSpecific
50 GraphicsServices GSEventRunModal
51 UIKitCore UIApplicationMain
52 tmp main /...
53 libdyld.dylib start

最佳答案

当然,当应用程序进入后台时,操作系统实际上会拍摄应用程序的照片,在滚动后台应用程序时用作预览。在我们的应用程序中,我们做了这样的事情:

- (void)deleteAllTemporaryFiles
{
RCTLogInfo(@"[Delegate] Deleting all temp files...");
[self deleteDirectoryFiles:NSTemporaryDirectory()];
[self deleteDirectoryFiles:NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]];
[self deleteDirectoryFiles:NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]];
}

- (void)deleteDirectoryFiles:(NSString *)directory
{
NSArray<NSString *> *temp = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directory error:NULL];
for (NSString *path in temp) {
if (/* check for directories you want to keep here */) continue;
@try {
NSString *filePath = [directory stringByAppendingPathComponent:path];
RCTLogInfo(@"[Delegate] Deleting %@",filePath);
[[NSFileManager defaultManager] removeItemAtPath:filePath error:NULL];
}
@catch (NSException *e) {
RCTLogInfo(@"[Delegate] Deletion error: %@", e);
}
}
}

在委托(delegate)中:

// Delete all temp files when app is paused
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self deleteAllTemporaryFiles];
}

关于ios - React Native 在 iOS 应用程序进入后台时将新的图像副本加载到内存中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58495890/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com