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

便宜上、すべての画像を A​​rray/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 のみがアプリに含まれ、すべての画像は配列で安全になります。

于 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];

-> 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;
}
于 2012-05-14T10:46:15.763 に答える