Large UIScrollView with background pattern fails

自作多情 提交于 2019-12-11 07:07:05

问题


I have a UIScrollView with a very large content view, it's a bookshelf with around 1000 items on it, when the scroll views content becomes more than around 16000px high it shows a black background and all graphics overlap/blend together.

Here is the sample code

- (void)viewDidLoad {
    [super viewDidLoad];

    // Doesn't work
    int sizeForContent = 20000;

    // Does Work
    //sizeForContent = 10000;


    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];  

    UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, sizeForContent)];
    subView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"wood.jpg"]];
    [scroll addSubview:subView];
    [subView release];

    scroll.contentSize = CGSizeMake(self.view.frame.size.width, sizeForContent);
    [self.view addSubview:scroll]; 

    [scroll release];

}

Does anyone know why this happens? I assume it is something to do with memory limits but it doesn't seem to be using much memory.

I know I could set the scrollview background color directly instead of adding a subview and setting the background of that but I need to leave the scrollview background as a color.

You can get the basic xcode project from http://cl.ly/2i3F3q0G1j3q433n0f1E if you want to see the issue.

Any help or explanations greatly appreciated.


回答1:


mhm, i also guess that it's a memory problem...

try to avoid this:

 subView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"wood.jpg"]];

i suppose that when you do that xCode load a small jpg, but then draw it for the whole area of your UIView that at the and will have a big "image" to keep in memory...

try to use just a fill color instead and see if something change...

if so a possible solution could be to use a CATiledLayer for your view, so just the visible portion of your big UIView will be in memory (with the inconvenience that users will see it drawing everytime they scroll around...)




回答2:


I also face the same problem. I have a long image, I just want to perform a panaroma effect on that while scrolling. If I used CATiledLayer, will it gives that smoothness. I dont think so will give that smoothness. Because it will draw everytime when it scrolls.I just want to do as the standard iPhone app icons scrolling the Home.




回答3:


I solved it by creating view with pattern background, and adding scroll view as subview with [UIColor clearColor] background.



来源:https://stackoverflow.com/questions/5204434/large-uiscrollview-with-background-pattern-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!