3

我已将图像动态添加到滚动视图。以下是代码:

-(void)displayimages {
    for (int i=0; i<[featuredarray count]; i++) {
        NSString *image=[[featuredarray objectAtIndex:i]objectForKey:@"img"];

        if (i==0) {
            webview=[[UIImageView alloc]initWithFrame:CGRectMake(i*290+15, 0, 270, 380)];
        }
        else {
            webview=[[UIImageView alloc]initWithFrame:CGRectMake(i*290+15, 0, 270, 380)];
        }
        webview.backgroundColor=[UIColor clearColor];
        webview.layer.borderWidth=4;
        webview.layer.cornerRadius=4;
        webview.layer.borderColor=[[UIColor whiteColor]CGColor];
        [webview setTag:i];
        webview.userInteractionEnabled=YES;
        NSURL *url = [NSURL URLWithString:image];
        [webview setImageWithURL:url placeholderImage:[UIImage 
           imageNamed:@"placeholder.png"]];
        [scroll addSubview:webview];

        UITapGestureRecognizer *tap1 =
        [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlefirstTap:)];
        tap1.delegate=self;
        tap1.numberOfTapsRequired=1;
        [webview addGestureRecognizer:tap1];
        [tap1 release];
        [webview release];
   }

   pages.defersCurrentPageDisplay=YES;
   scroll.delegate =self;

   scroll.contentSize = CGSizeMake(290*[featuredarray count],300);
   scroll.pagingEnabled = YES;
   pages.numberOfPages = [featuredarray count]-1;
   pages.currentPage =0;
}

-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    int gallerypage = scrollView.contentOffset.x /scrollView.frame.size.width;
    CGRect frame = scroll.frame;
    frame.origin.x = frame.size.width-15*gallerypage;
    NSLog(@"frame.origin.x..%f",frame.origin.x);
    frame.origin.y =0;
    [scroll scrollRectToVisible:frame animated:YES];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    int gallerypage = scrollView.contentOffset.x /scrollView.frame.size.width;
    CGRect frame = scroll.frame;
    frame.origin.x=gallerypage*290;
    frame.origin.y =0;
    [scroll scrollRectToVisible:frame animated:YES];
}

当我使用 pagecontroller 操作时,它可以很好地显示图像

-(IBAction)pagecontrolaction:(id)sender {
    int page = pages.currentPage;
    CGRect frame = scroll.frame;
    frame.origin.x=page*290;
    frame.origin.y =0;
    [scroll scrollRectToVisible:frame animated:YES];
}

但是当我使用触摸事件来进行图像查看时,它无法顺利显示图像。它在滚动过程中卡住。

这些是显示当前图像和第二张图像开头的预览。

4

8 回答 8

1

这两个方法:scrollViewWillBeginDecelerating 和 scrollViewDidEndDecelerating 包含两个动画,它们具有不同的 x 位置,它被动画到:

frame.origin.x = frame.size.width-15*gallerypage;

frame.origin.x=gallerypage*290;

最好关闭任一功能:scrollViewWillBeginDecelerating 或 scrollViewDidEndDecelerating。否则,您可能应该调整 x 位置(两个功能必须同步)它将被动画化。

于 2013-06-30T12:46:35.290 回答
0

当您尝试显示更大尺寸的图像时会发生这种情况,在这种情况下您必须使用缓存来获取图像并尝试从那里加载图像。为了更好地了解如何使用它..使用 SDwebimage 那是很好的方法这样做,它很快。你可以阅读

  1. 延迟加载

    2.分页启用。

https://github.com/rs/SDWebImage

需要任何进一步的帮助。很高兴为您提供帮助。

于 2013-07-07T04:40:28.747 回答
0
if (i==0) {
            webview=[[UIImageView alloc]initWithFrame:CGRectMake(i*320, 0, 270, 440)];
        }
        else {
            webview=[[UIImageView alloc]initWithFrame:CGRectMake(i*320, 0, 270, 380)];
        }
于 2013-06-27T07:05:54.717 回答
0

您应该检查SYPaginator(SYPaginator 是一个水平的 TableView,具有延迟加载和视图回收。)

简单的分页滚动视图,使复杂的任务更容易。我们在包括 Hipstamatic、D-Series 和 IncrediBooth 在内的几个 Synthetic 应用程序中使用了它。

如果您需要更多性能,您应该尝试一下MSCachedAsyncViewDrawing

Helper 类,允许您将视图 (a) 同步到具有缓存的 UIImage 以实现出色的性能。

于 2013-07-03T12:36:00.217 回答
0

我不确定你为什么在开始滚动时设置框架,但如果我在你的位置,我会按照以下逻辑执行:

-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    // do nothing
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    int gallerypage = scrollView.contentOffset.x /scrollView.frame.size.width;
    CGFloat xoffset = 290 * gallerypage;
    [UIView animateWithDuration:0.5
                     animations:^{
                         scrollView.contentOffset = CGPointMake(xoffset, 0);
                     } 
                     completion:^(BOOL finished){
                         NSLog(@"Done!");
                     }];
}

希望能帮助到你。

于 2013-07-07T09:22:45.290 回答
0

我认为发生这种情况是因为您的图像尺寸(MB)较大,并且您CGRect无需将它们调整为特定尺寸即可将它们放入其中,这也很重要您在滚动视图中加载了多少张图片,如果您加载了很多图片,它将需要一个很多内存,它会慢慢滑动。

于 2013-07-06T22:11:08.943 回答
0

在将图像加载到 UIImageView 之前,您绝对应该在后台调整图像大小。此外,您应该将 UIImageView 内容模式设置为不调整大小的模式。在其他方式已经实现(异步缓存和其他)之后,它给了我显着的滚动改进。

于 2013-07-12T20:38:25.893 回答
0

您需要更改从远程资源加载图像的实现,这就是最大的瓶颈所在。

我不想详细说明,但尝试替换这行代码

[webview setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

有一些对 CPU 更友好的东西。使用采用多线程的缓存机制,您要做的是将繁重的工作(下载远程图像)移至另一个线程。

SDWebImage 在这个 https://github.com/rs/SDWebImage上做得很好 如果你有点硬核,你可以使用你自己的多线程图像下载器

[NSURLConnection sendAsynchronousRequest:queue: completionHandler:] 

并在块中加载图像,甚至更好dispatch_async(queu, block)

于 2013-07-03T12:11:56.660 回答