2014-02-13 53 views
1

我在一个项目(iOS7 & ARC),其中,我想显示已存入沙箱目录中的滚动view.These图像的图像的N多工作。我的应用程序我现在面临一个问题,即滚动型不光滑只有横向,它坚持2-3次滚动滚动视图并不顺利

这是我如何配置ScrollView

[self.containerScroll setAutoresizesSubviews:NO]; 
self.containerScroll.pagingEnabled = YES; 
self.containerScroll.showsHorizontalScrollIndicator = NO; 
self.containerScroll.showsVerticalScrollIndicator = NO; 
self.containerScroll.scrollsToTop = NO; 
self.containerScroll.maximumZoomScale = 5.0; 
self.containerScroll.minimumZoomScale = 1.0; 
self.containerScroll.delegate = self; 

我一次只保留三个图像scrollView

我加载在ScrollView图片如下方法

-(void) loadScrollViewWithPage:(int) page{ 

if (page >= self.numberOfSlides) 
    return; 

float image_width; 
float image_height; 

if(self.isFromListView){ 

    if(IS_IPHONE5){ 
     image_width = 568.0f; 
     image_height = 320.0f; 
    } else{ 
     // iPhone retina-3.5 inch 
     image_width = 480.0f; 
     image_height = 320.0f; 
    } 

} 
else{ 
    image_width = IMAGE_WIDTH; 
    image_height = IMAGE_HEIGHT; 
} 

CGFloat xPos = page * image_width; 
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(xPos, 0.0f, image_width, image_height)]; 
imgView.tag = page; 
NSString *imgPath = [self.storageDirPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%d%@", page, Image_Extension_JPG]]; 

NSFileManager *fileManager = [NSFileManager defaultManager]; 
__block UIImage *img = nil; 
if(![fileManager fileExistsAtPath:imgPath]){ 
    [imgView setContentMode:UIViewContentModeCenter]; 
    img = [UIImage imageNamed:@"icon-loader.png"]; 
    [imgView setImage:img]; 
} 
else{ 
    [imgView setContentMode:UIViewContentModeScaleAspectFit]; 
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ 

       img = [[UIImage alloc] initWithCGImage:[[UIImage imageWithData:[NSData dataWithContentsOfFile:imgPath]] CGImage] scale:1.0 orientation:UIImageOrientationUp]; 

         dispatch_async(dispatch_get_main_queue(), ^{ 
         [imgView setImage:img]; 
       }); 

      }); 
} 

[self.containerScroll addSubview:imgView]; 
img = nil; 
fileManager = nil; 
imgView = nil; 

}

,这我ScrollView委托方法如何去...

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{ 
    self.containerScroll.scrollEnabled = YES; 

    float page = self.containerScroll.contentOffset.x/self.view.frame.size.width; 
    showingSlide = (UInt16) roundf(page); 

    if(scrollView == self.containerScroll){ 

     // switch the indicator when more than 50% of the previous/next page is visible 
     CGFloat pageWidth = CGRectGetWidth(self.containerScroll.frame); 
     NSUInteger pageNo = floor((self.containerScroll.contentOffset.x - pageWidth/2)/pageWidth) + 1; 

     // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling) 
     [self loadScrollViewWithPage:pageNo - 1]; 
     [self loadScrollViewWithPage:pageNo]; 
     [self loadScrollViewWithPage:pageNo + 1]; 

     // a possible optimization would be to unload the views+controllers which are no longer visible 

     if(scrollView == self.containerScroll) 
     { 
      [self.previewTableView reloadData]; 
      [self.previewTableView setContentOffset:CGPointMake(0, (page*220)+64) animated:NO]; 
      [self.previewTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:page inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; 

      [self updateSlideNumber]; 
      [self flashSlideNumber]; 
     } 

     //unload unnecessary imageviews from scroll view 
     for (UIView* view in self.containerScroll.subviews) { 
      if ([view isKindOfClass:[UIImageView class]] && view.tag != page && view.tag != page-1 && view.tag != page+1) { 
       [view removeFromSuperview]; 
      } 
     } 

    } 
} 

现在的问题是平滑scrollView。当我开始滚动时,它滚动得很好,但在2或3(或任何随机数之后)页面滚动后,它会卡住,尝试2-3次后,只会再次移动,我不得不轻轻滑动。提前致谢。

回答

0

我认为这是一个内存问题在你的代码中尝试@autorelease池。

+0

我已经试过这种方式...没有运气。 – Suryakant

0

使用scrollview不是一个好的方法来显示图像,我会建议你要么使用tableview或collevtionview相同。

因为scollview不会重复使用内存,所以应用程序的内存将随着每次滚动而不断增加,另一方面,tableview和collectionview会重用内存。

+0

你和@HookShot是正确的..应该使用TableView或集合视图,但在这个阶段的项目,我不能这样做...必须去与ScrollView。 – Suryakant

0

作为改善效果的最有效方法,在您监视应用程序中的内存使用情况时,确实很慢(一次一个)滚动。当每个新图像添加到视图中时,您都可以观看它,特别是如果您没有对图像进行任何优化。

另一件事是,虽然你的代码看起来像是释放图像,但你仍然需要记住,它仍然需要在你滚动时重新加载图像。你正在主线程上创建你的图像,所以你永远不会得到UITableView的平滑性。虽然我意识到您正在异步线程上创建图像视图,但添加和滚动它们的操作仍由主线程处理。

我会建议一个UITableView来解决你的问题,或UICollectionView。如果你使用scrollview,我会建议使用某种类型的破碎机,以尽可能小的图像尺寸,同时保持质量不错。

如果您需要关于TableView实现方面的帮助,您应该找到围绕SO的大量信息。如果你仍然希望它看起来像一个滚动视图,可能是一个不错的选择,只是使所有的分隔符,头文件等零,然后使用延迟加载的图像。

+0

一旦图像添加到ImageView我只是将它设置为零。它的问题是什么? – Suryakant

+0

我认为问题出现在图像的实际读取时间内,因为它们被加载到滚动中。你可以检查仪器,因为他们被加载,看看花费的时间来执行负载?我建议使用破碎机作为您的第一步 - 这可以解决您的所有问题,甚至不必更改代码太多。是否有可能已经声明了所有的UIImage,并且在滚动时将它们分配给UIImageView,以便图像已经存储在内存中,准备装入UIImageView的容器中? – crashlog

+0

http://imageoptim.com/ - 这是一个很好的imagecrusher的链接我用户的Xcode/Webstuff =] – crashlog

0

你犯了两个错误。起初:从不使用imageNamed作为非图形内容(例如,使用imageNamed作为按钮背景)。第二:你尝试实时加载一个大的图像。所以你滚动视图有滞后因此。如果您在显示滚动视图之前加载所有图像,则会出现胺化结束滞后现象。但是你可以得到记忆警告。所以,你需要优化它。附:对不起,我的英语

+0

你写了该死的英文很好..所以不必对不起 第一:imageNamed方法来图片时真实图像不存在,所以让我们不讨论这种情况。当imageNamed甚至没有获得单次调用时,我的ScrollView滞后。所以这不是问题。 第二:我不能将所有图像加载到内存中,因为我可以有任意数量的图像。 – Suryakant