在滚动条中显示多张照片

时间:2012-05-14 09:51:36

标签: iphone ios image memory-management uiscrollview

在应用程序中,我需要显示否。滚动条中的图像(计数未知)。我将所有图像直接放在滚动条中。但是开始接收内存警告级别2.请建议我如何操作。还有其他办法吗? 您还可以参考以下链接以更好地了解问题。

https://www.dropbox.com/sh/h0vwnhhx1acfcb5/W2TQ638udh

 -(void) getPortraitScrollReady
  {
    UIImageView *imageView;
    NSString *imageName;
    int x=0;
    for (NSNumber *numberObject in tagList)
    {
        imageName = [[NSString alloc] initWithFormat: @"%@", [self getImage:[numberObject intValue]]];
        imageView = [[UIImageView alloc ] initWithFrame: CGRectMake(x, 0, 320, 320)];
        imageView.image = [self thumbWithSideOfLength:320 pathForMain:imageName];   // a function that returns an image;

        [scrollPortrait addSubview:imageView];

        [imageName release];

        [imageView release];

        x+=320;
    }
    scrollPortrait.contentSize = CGSizeMake(x, 320);
    int pageNo =[self indexofObject:imageNo inArray:tagList]; 
    [scrollPortrait setContentOffset:CGPointMake(pageNo*320, 0) animated:NO];
 }

2 个答案:

答案 0 :(得分:2)

根据方便将所有图像保存在Array / MutableArray中。

采用Scrollview,在滚动视图中,使用5-UIImageViews显示数组中的图像。将UIScrollView的长度与5-UIImageViews匹配。

当在任何方向上完成滚动时,然后以相反的方向'释放'UIImageView&在滚动方向上“分配”一个新的UIImageVIew。

这就是我在项目中所做的。 .h文件内容

IBOutlet UIScrollView *scrlView;
UIImageView *imgView1;
UIImageView *imgView2;
UIImageView *imgView3;
UIImageView *imgView4;
UIImageView *imgView5;

NSMutableArray *imgGallery;
NSInteger mCount;
NSInteger centerImg;

CGFloat imgFrameHeight;
CGFloat imgView1X;
CGFloat imgView2X;
CGFloat imgView3X;
CGFloat imgView4X;
CGFloat imgView5X;

CGFloat previousOffsetX;
CGFloat currentOffsetX;

.m文件内容

- (void)photoGallery
{
    imgGallery = [[NSMutableArray alloc] initWithCapacity:10];
    [imgGallery addObject:[UIImage imageNamed:@"1.png"]];
    [imgGallery addObject:[UIImage imageNamed:@"2.png"]];
    [imgGallery addObject:[UIImage imageNamed:@"3.png"]];
    [imgGallery addObject:[UIImage imageNamed:@"4.png"]];
    [imgGallery addObject:[UIImage imageNamed:@"5.png"]];
    [imgGallery addObject:[UIImage imageNamed:@"6.png"]];
    [imgGallery addObject:[UIImage imageNamed:@"7.png"]];
    [imgGallery addObject:[UIImage imageNamed:@"8.png"]];
    [imgGallery addObject:[UIImage imageNamed:@"9.png"]];
}

- (void)photoScroller
{
    CGFloat appFrameHeight = self.view.frame.size.height;
    CGFloat navFrameHeight = self.navigationController.navigationBar.frame.size.height;
    CGFloat tabFrameHeight = self.tabBarController.tabBar.frame.size.height;
    imgFrameHeight = appFrameHeight - (navFrameHeight+tabFrameHeight);

    mCount = [imgGallery count];
    [scrlView setContentSize:CGSizeMake(320 * mCount, imgFrameHeight)];
    CGFloat offsetX = ((320 * mCount)/2) - 160;
    [scrlView setContentOffset:CGPointMake(offsetX, 0)];

    imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(offsetX - (320*2), 0, 320, imgFrameHeight)];
    imgView2 = [[UIImageView alloc] initWithFrame:CGRectMake(offsetX - 320, 0, 320, imgFrameHeight)];
    imgView3 = [[UIImageView alloc] initWithFrame:CGRectMake(offsetX, 0, 320, imgFrameHeight)];
    imgView4 = [[UIImageView alloc] initWithFrame:CGRectMake(offsetX + 320, 0, 320, imgFrameHeight)];
    imgView5 = [[UIImageView alloc] initWithFrame:CGRectMake(offsetX + (320*2), 0, 320, imgFrameHeight)];

    int center = mCount/2;
    int tmp = center * 2;
    int remainder = mCount - tmp;
    if (remainder != 0) {
        centerImg = center;
    } else {
        centerImg = center - remainder;
    }

    imgView1.image = [imgGallery objectAtIndex:(centerImg-2)];
    imgView2.image = [imgGallery objectAtIndex:(centerImg-1)];
    imgView3.image = [imgGallery objectAtIndex:centerImg];
    imgView4.image = [imgGallery objectAtIndex:(centerImg+1)];
    imgView5.image = [imgGallery objectAtIndex:(centerImg+2)];

    [scrlView addSubview:imgView1];
    [scrlView addSubview:imgView2];
    [scrlView addSubview:imgView3];
    [scrlView addSubview:imgView4];
    [scrlView addSubview:imgView5];
}

以下是调整UIImageViews的代码

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    imgView1X = imgView1.frame.origin.x;
    imgView2X = imgView2.frame.origin.x;
    imgView3X = imgView3.frame.origin.x;
    imgView4X = imgView4.frame.origin.x;
    imgView5X = imgView5.frame.origin.x;

    CGPoint previousOffset = [scrlView contentOffset];
    previousOffsetX = previousOffset.x;

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //get the current offset
    CGPoint currentOffset = [scrollView contentOffset];
    currentOffsetX = currentOffset.x;

    //NSLog(NSStringFromCGPoint(currentOffset));

    //calculate offset X of RIGHT ImageView to be shifted
    CGFloat identifyRightIV = previousOffsetX +640;
    //calcualte new LEFT offset X for shifting the previously calcualted RIGHT ImageView
    CGFloat newLX = identifyRightIV - (320*5);
    //calculate offset X of LEFT ImageView to be shifted
    CGFloat identifyLeftIV = previousOffsetX -640;
    //calcualte new RIGHT offset X for shifting the previously calcualted LEFT ImageView
    CGFloat newRX = identifyLeftIV + (320*5);
    //index of new LEFT Image in an array imgGallery
    int newImageL = newLX / 320;
    //index of new RIGHT Image in an array imgGallery
    int newImageR = newRX / 320;

    //if scrolled to left
    if (newLX >= 0) // Is shifting is necessary? i.e. if new LEFT offsetX is greater than lowest offsetX(0) of scrollview
    {
        if (currentOffsetX == (previousOffsetX-320))
        {
            if (imgView1X == identifyRightIV) {
                imgView1.frame = CGRectMake(newLX, 0, 320, imgFrameHeight);
                imgView1.image = [imgGallery objectAtIndex:newImageL];
            }
            else if (imgView2X == identifyRightIV) {
                imgView2.frame = CGRectMake(newLX, 0, 320, imgFrameHeight);
                imgView2.image = [imgGallery objectAtIndex:newImageL];
            }
            else if (imgView3X == identifyRightIV) {
                imgView3.frame = CGRectMake(newLX, 0, 320, imgFrameHeight);
                imgView3.image = [imgGallery objectAtIndex:newImageL];
            }
            else if (imgView4X == identifyRightIV) {
                imgView4.frame = CGRectMake(newLX, 0, 320, imgFrameHeight);
                imgView4.image = [imgGallery objectAtIndex:newImageL];
            }
            else if (imgView5X == identifyRightIV) {
                imgView5.frame = CGRectMake(newLX, 0, 320, imgFrameHeight);
                imgView5.image = [imgGallery objectAtIndex:newImageL];
            }
        }
    }
    //if scrolled to right
    if (newRX < (mCount*320))   // Is shifting is necessary? i.e. if new RIGHT offsetX is less than highest offsetX of scrollview
    {
        if (currentOffsetX == (previousOffsetX+320))
        {
            if (imgView1X == identifyLeftIV) {
                imgView1.frame = CGRectMake(newRX, 0, 320, imgFrameHeight);
                imgView1.image = [imgGallery objectAtIndex:newImageR];
            }
            else if (imgView2X == identifyLeftIV) {
                imgView2.frame = CGRectMake(newRX, 0, 320, imgFrameHeight);
                imgView2.image = [imgGallery objectAtIndex:newImageR];
            }
            else if (imgView3X == identifyLeftIV) {
                imgView3.frame = CGRectMake(newRX, 0, 320, imgFrameHeight);
                imgView3.image = [imgGallery objectAtIndex:newImageR];
            }
            else if (imgView4X == identifyLeftIV) {
                imgView4.frame = CGRectMake(newRX, 0, 320, imgFrameHeight);
                imgView4.image = [imgGallery objectAtIndex:newImageR];
            }
            else if (imgView5X == identifyLeftIV) {
                imgView5.frame = CGRectMake(newRX, 0, 320, imgFrameHeight);
                imgView5.image = [imgGallery objectAtIndex:newImageR];
            }
        }

    }
}

由于在整个生命周期中只有5-UIImageViews将在App&amp;所有图像都是安全的阵列。

答案 1 :(得分:1)

尝试第一个问题的代码:

          scrl=[[UIScrollView alloc]initWithFrame:CGRectMake(10, 92, 300, 370)];
        //scrl.backgroundColor=[UIColor blackColor];
        scrl.delegate=self;
        scrl.showsHorizontalScrollIndicator=NO;
        scrl.showsVerticalScrollIndicator=NO;
            scrl.pagingEnabled=YES;
        scrl.layer.cornerRadius = 5.0f;
        scrl.layer.masksToBounds = YES;
        scrl.layer.borderWidth = 5;
        scrl.layer.borderColor = [UIColor blackColor].CGColor;

        int i, x=0,y=0;
        for (i=0; i<[imageName count]; i++) 
        {

            imagBack=[[UIImageView alloc] initWithFrame:CGRectMake(x,y,300, 370)];
            imagBack.image=[UIImage imageNamed:[imageName objectAtIndex:i]];
            [scrl addSubview:imagBack];

             x =x+300;
       }
        scrl.contentSize=CGSizeMake(x,370);

        [self.view addSubview:scrl];

- &GT;第2期,你使用图片进行压缩(.png格式)图片是,试试这个:

   [self imageWithImage:image CovertToSize:CGSizeMake(46, 44)];

    NSData* imageData = UIImageJPEGRepresentation(image, 1.0f);//10 % quality compressed

保存图片时调用此方法:

-(UIImage *)imageWithImage:(UIImage *)image CovertToSize:(CGSize)size {
    UIGraphicsBeginImageContext(size);
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    destImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
    return destImage;
}