gpt4 book ai didi

ios - 如何优化具有大量图像的 UIScrollView

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:50:57 26 4
gpt4 key购买 nike

我一次加载了 UIScrollView 中的所有图像,我知道这是不好的方法,那么有没有更好的方法来优化它?

最佳答案

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
int currentPage = (scrollView.contentOffset.x / scrollView.frame.size.width);

// display the image and maybe +/-1 for a smoother scrolling
// but be sure to check if the image already exists, you can
// do this very easily using tags
if ([scrollView viewWithTag:(currentPage + 1)]) {
return;
} else {
// view is missing, create it and set its tag to currentPage+1
UIImageView *iv = [[UIImageView alloc] initWithFrame:
CGRectMake((currentPage + 1) * scrollView.frame.size.width,
0,
scrollView.frame.size.width,
scrollView.frame.size.height)];
iv.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.jpg",
currentPage + 1]];
iv.tag = currentPage + 1;
[sv addSubview:iv];
}

/**
* using your paging numbers as tag, you can also clean the UIScrollView
* from no longer needed views to get your memory back
* remove all image views except -1 and +1 of the currently drawn page
*/
for (int i = 0; i < 50; i++) {
if ((i < (currentPage - 1) || i > (currentPage + 1)) &&
[scrollView viewWithTag:(i + 1)]) {
[[scrollView viewWithTag:(i + 1)] removeFromSuperview];
}
}
}

关于ios - 如何优化具有大量图像的 UIScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10221591/

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