ios - 延迟加载 UIScrollView - 如何使滚动更流畅
<p><p>我已经在这样的 ScrollView 中完成了延迟加载:</p>
<pre><code>-(void)scrollViewDidScroll:(UIScrollView *)myScrollView {
int currentPage = (1 + myScrollView.contentOffset.x / kXItemSpacingIphone);
for (ItemView* itemView in ){
if (itemView.tag >= currentPage-2 && itemView.tag <= currentPage+2)
{
//keep it visible
if (!itemView.isLoaded) {
];
}
}
else
{
//hide it
if (itemView.isLoaded) {
;
}
}
}
}
</code></pre>
<p>如果 View 距离屏幕显示 +/- 2 个“页面”,则基本上会加载 View 。这大大减少了我正在使用的内存量(而不是一次加载 20 多个 ItemView),这很好。但是,所有加载/卸载确实使滚动有点不稳定,尤其是在速度较慢的设备上。这是 ItemView 加载时实际发生的情况:</p>
<pre><code>- (void)layoutWithData:(Item*)_data {
self.data = _data;
//grab the image from the bundle
UIImage *img;
NSString *filePath = [ pathForResource:_data.image ofType:@"jpg"];
if(filePath.length > 0 && filePath != (id)) {
img = ;
}
UIButton *btn = ;
;
;
btn.frame = CGRectMake(0, 0, kItemPosterWidthIphone, kItemPosterHeightIphone);
;
self.isLoaded = YES;
}
</code></pre>
<p>以及ItemView的卸载:</p>
<pre><code>- (void)unloadData{
for(UIView *subview in ) {
;
}
self.data = nil;
self.isLoaded = NO;
}
</code></pre>
<p>再次,我可以做些什么来加快加载/卸载速度,从而使 UIScrollView 更流畅?</p>
<hr/>
<p>尝试异步:</p>
<pre><code>- (void)layoutWithData:(Item*)_data {
self.data = _data;
self.isLoaded = YES;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
UIImage *img;
NSString *filePath = [ pathForResource:_data.image ofType:@"jpg"];
if(filePath.length > 0 && filePath != (id)) {
img = ;
}
dispatch_async(dispatch_get_main_queue(), ^{
UIButton *btn = ;
;
;
btn.frame = CGRectMake(0, 0, kItemPosterWidthIphone, kItemPosterHeightIphone);
self.imageView = btn;
;
});
});
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>不久前我遇到了这个问题,我将加载图像从磁盘移动到后台线程。试试看是否更快。</p>
<p>请看这里:<a href="https://stackoverflow.com/questions/8016235/loading-images-from-disk-in-iphone-app-is-slow" rel="noreferrer noopener nofollow">loading images from disk in iPhone app is slow</a> </p>
<p>编辑:图像加载滞后也可能是由于 UIImages 的延迟处理造成的</p>
<p>请看这里:<a href="https://stackoverflow.com/questions/10790183/setting-image-property-of-uiimageview-causes-major-lag" rel="noreferrer noopener nofollow">Setting image property of UIImageView causes major lag</a> </p></p>
<p style="font-size: 20px;">关于ios - 延迟加载 UIScrollView - 如何使滚动更流畅,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/12828169/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/12828169/
</a>
</p>
页:
[1]