Pet*_*ete 1 performance image objective-c uitableview ios
我正在显示大量直接从我的应用程序加载的图像(未下载).第一次滚动时我的表视图很慢.显示所有单元格后,它变得平滑.我真的不知道为什么.
我有一个UIImage数组,我正在viewDidLoad中加载.然后在我的tableview委托中,我只是在给定的索引路径上获取图像并将其设置为我的单元格的UIImageView.
你知道如何提高表现吗?
小智 5
只是为了分享我已经修好并且工作得非常好我遵循的步骤.
1)设置performSelectorInBAckground函数,并将Cell作为参数传递,该参数保存滚动视图或uiview以放置许多iamges.
2)在后台功能中使用imagewithContents文件加载从应用程序包或本地文件存储的图像.
3)使用此代码将图像设置为imageView.
//// Start of optimisation - for iamges to load dynamically in cell with delay , make sure you call this function in performSelectorinBackground./////
//Setting nil if any for safety
imageViewItem.image = nil;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
UIImage *image = // Load from file or Bundle as you want
dispatch_sync(dispatch_get_main_queue(), ^{
//Set the image to image view not, wither cell.imageview or [cell add subviw:imageview later ]
[imageViewItem setImage:image];
[imageViewItem setNeedsLayout];
});
});
//// End of optimisation/////
Run Code Online (Sandbox Code Playgroud)
这将动态加载所有图像,并且还比以前的慢速和不稳定行为更平滑地滚动表格视图.
祝一切顺利