简体   繁体   English

如何在iPhone的滚动视图中动态加载我的视图中的图像

[英]how to load the images in my view dynamically in a scroll view in iPhone

i want to display many images like thumbnails in a scroll view and and i want the images displayed dynamically we scrolls down or left like a table view cells 我想在滚动视图中显示许多图像,例如缩略图,并且我希望动态显示的图像像表视图单元格一样向下或向左滚动

can u please tell how to that... 你能告诉我如何...

Thanks 谢谢

with the following code..when we scroll the scroll view im calling this code and able to display the images dynamically (which r only visible) but the problem is.. while scrolling with scroll bars im getting the two images..vertically and horizontally..its only happening when i scroll.. can any body help me out please..? 与下面的代码..当我们滚动滚动视图时调用此代码并能够动态显示图像(仅可见),但问题是..当使用滚动条滚动时,获得垂直和水平两个图像..它只会在我滚动时发生..任何身体都可以帮助我..吗?

int tileSize;
    int imgSize;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
        tileSize = 255;
        imgSize = 247;
    }else{
        tileSize = 120;
        imgSize = 116;
    }
    CGRect visibleBounds = [songsContainer bounds];
    for (UIView *tile in [songsContainer subviews]) {
        CGRect scaledTileFrame =[tile frame];
        if (! CGRectIntersectsRect(scaledTileFrame, visibleBounds)) {
            for(UIView *view in [tile subviews])
                [view removeFromSuperview];
            [recycledCells addObject:tile];
            [tile removeFromSuperview];
        }
    }
    int maxRow =[songsDict count]-1; // this is the maximum possible row
    int maxCol = noOfRowsInCell-1;  // and the maximum possible column
    int firstNeededRow = MAX(0, floorf(visibleBounds.origin.y / tileSize));
    int firstNeededCol = MAX(0, floorf(visibleBounds.origin.x / tileSize));
    int lastNeededRow  = MIN(maxRow, floorf(CGRectGetMaxY(visibleBounds) / tileSize));
    int lastNeededCol  = MIN(maxCol, floorf(CGRectGetMaxX(visibleBounds) / tileSize));
    NSLog(@".........MaxRow-%d,MaxCol-%d,firstNeddedRow-%d,firstNeededcol-%d,lNR-%d,lNC%d",maxRow, maxCol, firstNeededRow,firstNeededCol,lastNeededRow,lastNeededCol);
    // iterate through needed rows and columns, adding any tiles that are missing
    for (int row = firstNeededRow; row <= lastNeededRow; row++) {
        NSMutableArray *tempArray = (NSMutableArray *)[songsDict objectAtIndex:row];
        for (int col = firstNeededCol; col <= lastNeededCol ; col++) {
            BOOL tileIsMissing = (firstVisibleRow > row || firstVisibleColumn > col || 
                                  lastVisibleRow  < row || lastVisibleColumn  < col);
            if (tileIsMissing) {
                UIView *tile = (UIView *)[self dequeueReusableTile];

                if (!tile) {
                    // the scroll view will handle setting the tile's frame, so we don't have to worry about it
                    tile = [[[UIView alloc] initWithFrame:CGRectZero] autorelease]; 
                    tile.backgroundColor = [UIColor clearColor];
                }
                //tile.image = image for row and col;
                // set the tile's frame so we insert it at the correct position
                CGRect frame = CGRectMake(tileSize * col, tileSize * row, imgSize, imgSize);
                tile.frame = frame;
                if(col<[tempArray count])
                    [self addContentForTile:tile:row:col];
                else tile.backgroundColor = [UIColor clearColor];
                [songsContainer addSubview:tile];

            }
        }
    }
    firstVisibleRow = firstNeededRow+1; firstVisibleColumn = firstNeededCol+1;
    lastVisibleRow  = lastNeededRow;  lastVisibleColumn  = lastNeededCol;

Start with a scroll view and add each image in an UIImageView as a subview to the scrollview at a certain location. 从滚动视图开始,然后将UIImageView中的每个图像作为子视图添加到特定位置的滚动视图。

One thing to have in mind is to only hold in memory the images that are currently shown and their immediate neighbours. 要记住的一件事是仅将当前显示的图像及其直接邻居保留在内存中。

What you have to do is create your scrollview however you want. 您要做的就是根据需要创建滚动视图。 You have to decide whether you want a grid layout, or linear layout. 您必须决定是要使用网格布局还是线性布局。 In addition, if grid, do you want it locked to the horizontal bounds, locked to vertical, so it scrolls either vertical or horizontal, respectively. 另外,如果是网格,是否要将其锁定在水平范围内,锁定在垂直范围内,以便分别滚动垂直或水平方向。

Once you have that sorted out, then what I recommend is taking the architecture similar to how a tableview functions. 整理好之后,我建议采用的架构类似于表视图的功能。 That is, create individual "cells" that will hold your thumbnails. 也就是说,创建将保存缩略图的单个“单元格”。 Once you have these cells, you add them as subviews of your scrollview, at certain offsets (you need to do some math on the x/y planes). 一旦有了这些单元格,就可以将它们作为滚动视图的子视图添加到特定的偏移处(您需要在x / y平面上做一些数学运算)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM