2013-03-14 60 views
0

在我的iPhone应用程序中,我从Web服务器获取约300张图像url,我需要以3x3行和列格式在UIScrollView上显示所有图像。一旦我从Web服务器获得所有的URL,将它们转换为UIImage并显示图像。一切工作正常,但加载200图像应用程序崩溃后,我已经读了很多与UIScrollView有关的内存类型的问题,所以我们如何处理这个问题。我也读过苹果文档,但不清楚。如何在iPhone中的UIScrollView中动态添加图像

+0

可能重复崩溃](http://stackoverflow.com/questions/6786614/uiscrollview-lazy-loading-of-images-to-reduce-memory-usage-and-avoid-crash) – 2013-03-14 10:02:45

回答

-1

我不明白为什么你正在做的是一个问题。可能是你有记忆问题。在这种情况下,模拟器和设备结果应该不同。

如果这是一个内存问题,您应该使用tableview而不是滚动视图。在每个表格视图单元格中放置3个图像,并且应该这样做。

CDT

0

我得到的印象是,你是在一次下载所有300个图像融合在一起,并在内存中保持并增加了UIScrollView的..

有一个叫LazyLoad技术,手段不加载所有的图像一次,队列中加载它..

您可以参考此链接http://lazyloadinginuiscrollviewiniphone.blogspot.in/2011/12/iphone-uiscrollview-lazy-image-loading.html它可以帮助你实现一切顺利..

所有最优秀的

+0

是的,我正在阅读图像并显示它们UIScrollView动态增加uiscrollview的内容大小,一旦内存不足就会崩溃。 – user748001 2013-03-14 10:02:14

+0

做它懒惰的方式.. – iphonic 2013-03-14 10:03:56

0

您是否发布了不再可见的元素? UITableView可以做到这一点(这就是Sandro建议它的原因)。 你的UIScrollView的实现应该这样做。

您也可以考虑UICollectionViewGMGridView网格实现考虑到这一点。

1

这里是解决方案,我已经在我的应用程序中实现了它。这里是我的代码 sv_port是我的scrollView。 下载SDWebImageDownloader类文件并将其导入到您的项目中。在您的项目中添加相关框架。像imageIO.framework,QuartzCore

现在,在您的m添加

#import "UIImageView+WebCache.h" 
#import "SDImageCache.h" 
#import "SDWebImageCompat.h" 

//function for adding image in your scrollview 
-(void)populateImages 
{ 
int x=6,y=6; 

for(UIView * view in sv_port.subviews){ 
    if([view isKindOfClass:[UIImageView class]]) 
    { 
     [view removeFromSuperview]; view = nil; 
    } 
    if([view isKindOfClass:[UIButton class]]) 
    { 
     [view removeFromSuperview]; view = nil; 
    } 
} 

for(int p=0;p<[Manufacturer count];p++) 
{ 
    UIButton *btnImageButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    btnImageButton.frame = CGRectMake(x,y,70,70); 
    [btnImageButton setTag:p]; 
    [btnImageButton addTarget:self action:@selector(nextview:) forControlEvents:UIControlEventTouchUpInside]; 

    UIActivityIndicatorView *spinny = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
    spinny.frame=CGRectMake(btnImageButton.frame.origin.x+25,btnImageButton.frame.origin.y+25, 20, 20); 
    spinny.backgroundColor=[UIColor clearColor]; 
    [spinny startAnimating]; 

    [sv_port setScrollEnabled:YES]; 
    [sv_port addSubview:spinny]; 

    UIImageView *asyncImageView = [[[UIImageView alloc] initWithFrame:btnImageButton.frame] autorelease]; 
    asyncImageView.backgroundColor=[UIColor clearColor]; 
    CALayer *layer; 
    layer = asyncImageView.layer; 
    layer.borderWidth = 0.5; 
    layer.cornerRadius = 5; 
    layer.masksToBounds = YES; 


    [asyncImageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", yourUrl] ] placeholderImage:[UIImage imageNamed:nil] options:0 andResize:CGSizeMake(btnImageButton.frame.size.width,btnImageButton.frame.size.height) withContentMode:UIViewContentModeScaleAspectFit]; 
    asyncImageView.contentMode = UIViewContentModeScaleAspectFit; 

    [sv_port addSubview:btnImageButton]; 
    [sv_port addSubview:asyncImageView]; 
    int imgCntPort=0; 

    imgCntPort=(sv_port.frame.size.width/(asyncImageView.frame.size.width)); 

    ////NSLog(@"imgport %d",imgCntPort); 
    if((p+1)%imgCntPort==0) 
    { 
     x=5; 
     y=y+80; 
    } 
    else 
    { 
     x=x+80; 
    } 
} 
sv_port.contentSize = CGSizeMake(0, y); 
glob_x =0; glob_y =y; 
} 

希望它有助于..

+0

制造商是你的网址阵列。 – Dhruvik 2013-03-15 05:16:26

1
-(void)scrollimage 
{ 
    [AsyncImageLoader sharedLoader].cache = nil; 
    NSArray *viewsToRemove = [_scrollview_gallary subviews]; 
    for (UIView *v in viewsToRemove) { 
     [v removeFromSuperview]; 
    } 

    CGFloat width =_scrollview_gallary.bounds.size.width; 

    _scrollview_gallary.contentSize=CGSizeMake(width*[tmpArr count]-1, 360); 
    [_scrollview_gallary setShowsHorizontalScrollIndicator:NO]; 
    [_scrollview_gallary setShowsVerticalScrollIndicator:NO]; 
    int x=0; 
    int y=0; 
    for (int i=0; i<[tmpArr count]; i++) 
    { 
     AsyncImageView *imgv; 
     imgv =[[AsyncImageView alloc] initWithFrame:CGRectMake(x,y,width,360)]; 
     imgv.contentMode = UIViewContentModeScaleToFill; 
     imgv.activityIndicatorStyle = UIActivityIndicatorViewStyleWhite; 
     NSString *strimag=[NSString stringWithFormat:@"%@",[tmpArr objectAtIndex:i]]; 
     imgv.imageURL=[NSURL URLWithString:[tmpArr objectAtIndex:i]]; 
     [_scrollview_gallary addSubview:imgv]; 
     x=x+width; 
    } 
    _pageview.numberOfPages=([tmpArr count]); 
    _scrollview_gallary.pagingEnabled = YES; 
} 
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{ 
    int page; 
    CGFloat pageWidth = _scrollview_gallary.frame.size.width; 
    page = floor((_scrollview_gallary.contentOffset.x - pageWidth/2)/pageWidth) + 1; 
    self.pageview.currentPage = page; 

} 
图像[UIScrollView的延迟加载,以减少内存使用,避免
相关问题