开发者

ScrollView with large images scrolls too slowly

When I am scrolling images frequently in a UIScrollView then after some images, the next image t开发者_高级运维akes time to load... it's not taking too much time but looks odd.

Suppose I have 27 images in a scrollView. When I start to scroll these images, for 1 or 2 images it scrolls smoothly but when I scroll again to see the 3rd image it takes time to load. Then when I start the images scrolling again from the 3rd image, it behaves like before.

I can't load all 27 images at a time or my app crashes.

When I slowly scroll the scrollview then I don't have this problem.

My code is below:

//Taking image view for 27 images;   

int x=0;   
for(int i = 1; i<=27; i++) {                
    imageView = [[UIImageView alloc] init];
    imageView .frame = CGRectMake(x,0,768,1024);
    imageView.tag=i;
    imageView.image=nil;
    imageView.userInteractionEnabled=YES;
    [contentView addSubview:imageView];
    x+=768;     
}

//setContentOffset of the scrollView -->ContentView

[contentView setContentOffset: CGPointMake((imageNumber-1)*768, 0) animated: YES];

//desire image which i want to see from the start of the scrollview
pageNumber=imageNumber;


int pageBefore=pageNumber-1;
int pageAfter=pageNumber+1;

//Views for image 

for( UIImageView * views in [contentView subviews]){
    if(views.tag==pageNumber){
        if(views.image==nil){
            NSLog(@"entering");
            views.image=[UIImage imageNamed:[ NSString stringWithFormat:@"%d.jpg",pageNumber]];
        [views.image release];
        }
    }

    if(views.tag==pageBefore){
        if(views.image==nil){
            views.image=[UIImage imageNamed:[ NSString stringWithFormat:@"%d.jpg",pageBefore]];
        [views.image release];
        }
    }

    if(views.tag==pageAfter){
        if(views.image==nil){
            views.image=[UIImage imageNamed:[ NSString stringWithFormat:@"%d.jpg",pageAfter]];
        [views.image release];
        }
    }


My alarm bells rang when I saw this;

imageView .frame = CGRectMake(x,0,768,1024);

Apart from the space before .frame, are you saying that your images are 768x1024? That's HUGE and I suspect your problems are memory ones rather than code ones.

Be aware that in particular, using UIImage imageNamed: is likely to cause grief with such large images as that method caches the images in memory. You may wish to consider using alternative methods that load the image from a file each time.


You should try use the EGOImageView, it has caching build in which might help with your performance issues. You can implement a placeholder image to show the user that an image is being prepared for viewing. The image will load in another thread before being displayed, giving you smoother scrolling performance. The EGOImageView is part of the EGOImageLoading library.

https://github.com/tastefulworks/EGOImageLoading

As an alternative you could create your own lazy loading mechanism to increase scrolling performance. E.g. once a user stops scrolling for a second, start loading the image, otherwise display placeholder image if not yet the correct image is cached.

Edit: when thinking more about this issue, I realize caching won't help much (since you already load image from disk), but the asynchronous loading of images should help with the scroll performance, so make use of NSThread or NSOperation to load the image in a background thread, then notify the main thread that the image is loaded and ready for display.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜