1

在应用程序中,我需要显示否。滚动器中的图像(计数未知)。我将所有图像直接放在滚动条中。但是开始收到内存警告级别 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];
 }
4

2 回答 2

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 中,所有图像在数组中都是安全的。

于 2012-05-14T10:52:00.690 回答
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];

-> 第二期,您使用要压缩的图像(.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;
}
于 2012-05-14T10:46:15.763 回答