带有大图像的 ScrollView 滚动速度太慢

发布于 2024-11-02 04:31:22 字数 1670 浏览 0 评论 0原文

当我在 UIScrollView 中频繁滚动图像时,在一些图像之后,下一个图像需要一些时间来加载......它并没有花费太多时间,但看起来很奇怪。

假设我的滚动视图中有 27 个图像。当我开始滚动这些图像时,对于 1 或 2 个图像,它可以平滑地滚动,但是当我再次滚动以查看第三个图像时,它需要一些时间来加载。然后,当我从第三张图像开始再次滚动图像时,它的行为就像以前一样。

我无法一次加载全部 27 张图像,否则我的应用程序会崩溃。

当我缓慢滚动滚动视图时,就没有这个问题了。

我的代码如下:

//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];
        }
    }

When I am scrolling images frequently in a UIScrollView then after some images, the next image takes 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];
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长发绾君心 2024-11-09 04:31:22

当我看到这一幕时,我的警钟敲响了;

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

除了 .frame 之前的空格之外,您是说您的图像是 768x1024 吗?这是巨大的,我怀疑你的问题是内存问题而不是代码问题。

请注意,特别是使用 UIImage imageNamed: 可能会对如此大的图像造成麻烦,因为该方法将图像缓存在内存中。您可能希望考虑使用每次从文件加载图像的替代方法。

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.

葬花如无物 2024-11-09 04:31:22

您应该尝试使用 EGOImageView,它具有缓存内置功能,可能有助于解决您的性能问题。您可以实现占位符图像来向用户显示图像正在准备查看。图像将在显示之前加载到另一个线程中,从而为您提供更流畅的滚动性能。 EGOImageView 是 EGOImageLoading 库的一部分。

https://github.com/tastefulworks/EGOImageLoading

作为替代方案,您可以创建自己的延迟加载机制提高滚动性能。例如,一旦用户停止滚动一秒钟,就开始加载图像,否则如果尚未缓存正确的图像,则显示占位符图像。

编辑:当更多地考虑这个问题时,我意识到缓存不会有太大帮助(因为您已经从磁盘加载图像),但是图像的异步加载应该有助于提高滚动性能,因此请使用NSThread 或 NSOperation 在后台线程中加载图像,然后通知主线程图像已加载并准备显示。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文