leo*_*leo 2 iphone cocoa-touch uiviewcontroller uiview ipad
我有两个视图,第一个是一个简单的视图,有一些关于使用的介绍,点击一个按钮就可以打开主视图.主视图有许多图像和两个自定义表,其中行由文本和图像组成,因此主视图的创建非常慢.探查器显示大部分时间由imageIO消耗(decode_mcu,png_read_filter_row,pmap_copy_page和decompress_onepass)
我想通过在加载第一个视图后立即创建主视图进行优化,当我单击该按钮时,它只是将该视图设置为可见或将该视图置于前面.
我试图在第一个视图的viewDidLoad中分配&初始化主视图
- (void)viewDidLoad
{
[super viewDidLoad];
rootVC = [[RootViewController alloc] initWithNibName:@"ViewController" bundle:nil];
rootVC.delegate = self;
}
Run Code Online (Sandbox Code Playgroud)
并在按钮的操作方法中执行此操作
- (IBAction)buttonUp:(id)sender {
[self.view addSubview: rootVC.view];
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用,装载仍然需要很长时间.我怎么能牺牲一些记忆来提供更好的用户体验?
谢谢
狮子座
你应该:
使用Instruments查看代码花费最多时间的位置,并在那里进行优化.它可能不是你想的地方.
如果您正在使用UITableView,请了解如何按需创建UITableViewCells(而不是预加载大量数据)和循环实例(而不是重新创建它们).
只有当Instruments指出这是瓶颈时:尝试在后台队列中加载/解压缩图像,然后在加载后更新主队列上的UIImageView(或其他).
在iOS 4.x或更高版本中,您可以执行以下操作:
NSString *path = ...
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, NULL), ^{
// This is happening in the background thread.
UIImage *image = [UIImage imageWithContentsOfFile:path];
dispatch_async(dispatch_get_main_queue(), ^{
// This is happening on the main thread; all UI updates must happen here.
imageView.image = image;
});
});
Run Code Online (Sandbox Code Playgroud)
PS顺便说一下,UIViewController不会在创建时加载视图,而是按需加载.因此,基于上面的代码,表达式rootVC.view可能会触发加载视图.如果你想提前加载它,只需把它rootVC.view放在其他地方.