2011-01-05 9 views
0

次のコードに問題があります。2つ以上のボタンでUIImageViewを使用できないのはなぜですか?

1つ以上のボタンで1つのUIImageViewを使用したいのですが、UIImageViewを1回だけ定義し、必要な回数だけ使用することは論理的です。

imgView1を2つの異なるボタンに接続しようとすると、そのうちの1つだけが表示されます。

コメントアウトされたimgView2を使用して、これをラウンドすることができます。

しかし、私はこれを行う必要があるように思われる。なぜ私はただ1つのUIImageViewを持っていて、それを必要なだけ多くのビューに追加できないのですか?

// This will not allow me to use imgView1 more than once and only one of the imgViews/buttons is displayed. 
UIImage *image   = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"some_image" ofType:@"png"]]; 
    UIImageView *imgView1 = [[UIImageView alloc] initWithImage:image]; 
    //UIImageView *imgView2 = [[UIImageView alloc] initWithImage:image]; 

    CGRect frame = CGRectMake(10, 10, 70, 72); 

    UIButton *h=[UIButton buttonWithType:UIButtonTypeCustom]; 
    [h setFrame:frame]; 
    [h insertSubview:imgView1 atIndex:1]; 

    frame = CGRectMake(100, 10, 70, 72); 

    UIButton *h2=[UIButton buttonWithType:UIButtonTypeCustom]; 
    [h2 setFrame:frame]; 
    [h2 insertSubview:imgView1 atIndex:1]; 

    [self.view addSubview:h]; 
    [self.view addSubview:h2]; 

    [imgView1 release]; 
    //[imgView2 release]; 

編集:私は実際の後ろに座っているグラデーション背景レイヤーとイメージを作成しようとしています私は私の質問

にいくつかのコンテキストを追加しました

コンテキスト

画像であり、これに対するフォローアップの質問です(URL:Making a Transparent image button with bgcolor

私は単純にsetImageを使うことができます。グラデーションの背景レイヤをバックグラウンドに置くことはありません。例えば

int fullFrameX = 25; 
int extFrameX = fullFrameX + 3; 

CGRect fullFrame; 
CGRect frame; 

fullFrame = CGRectMake(fullFrameX, 10, 70,72); 
frame = CGRectMake(extFrameX, 13, 63,65); 

UIButton *h=[UIButton buttonWithType:UIButtonTypeCustom]; 
[h setFrame:fullFrame]; 
[[h layer] setMasksToBounds:YES]; 
//[h setBackgroundImage:image forState:UIControlStateNormal]; 
[h setImage:image forState:UIControlStateNormal]; 
[h setShowsTouchWhenHighlighted:YES]; 
[h setClipsToBounds:YES]; 


CAGradientLayer *gradientLayer = [[CAGradientLayer alloc] init]; 
[gradientLayer setBounds:frame]; 
[gradientLayer setPosition:CGPointMake([h bounds].size.width/2, [h bounds].size.height/2)]; 
[gradientLayer setColors:[NSArray arrayWithObjects: 
          (id)[[UIColor colorWithRed:255 green:0 blue:255 alpha:0]CGColor],(id)[[UIColor blackColor] CGColor], nil]]; 
[[h layer] insertSublayer:gradientLayer atIndex:1]; 

//[h insertSubview:imgView atIndex:1]; 

[h setTag:0]; 
[h addTarget:self action:@selector(btnSelectColor:) forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:h]; 
//[imgView release]; 

[gradientLayer release]; 

これはそのやっは、その私のグラデーションの背景と同じレベルに画像を入れているものを、(前の質問から)私の元のコードのcanabalisedバージョンです。

私が直面している問題を除いて、UIImageViewsを使ってほとんどの重い作業を行う答えを暗示しました。私は実際にそれを一度作成する必要があるときに複数のUIImageViewsを作成する必要があります。

アイデアは4つのボタンを持っていて、各ボタンはユーザーがクリックしてカラースキームを選択できるようになっています。

説明するのは少し難しいですが、私は試してそれをリストに分解します。

  1. 画像(アバター)は上部層
  2. ある勾配の背景が背景にあり、オブジェクト全体は、私は1つのだけUIImageViewを初期化する必要がなければならない
  3. クリック可能であるframe
  4. に押し込まれますたびに私はそれを使用したいが、同じUIImageViewの複数のコピーを作成しなければならない。

これは私の問題の背景を明確にしたいと考えています。

答えて

0

http://coffeeshopped.com/2010/09/iphone-how-to-dynamically-color-a-uiimage

上記のURLは私が今、問題を解決する助けました。

-(UIImage *)createGradientImage:(NSString *)name withTopColor:(CGColorRef)topColor withBottomColor:(CGColorRef)bottomColor 
{ 
    UIImage *image  = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name ofType:@"png"]]; 
    // load the image 
    // begin a new image context, to draw our colored image onto 
    UIGraphicsBeginImageContext(image.size); 

    // get a reference to that context we created 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // set the fill color 
    //UIColor *color = [UIColor redColor]; 
    //[color setFill]; 

    // translate/flip the graphics context (for transforming from CG* coords to UI* coords 
    CGContextTranslateCTM(context, 0, image.size.height); 
    CGContextScaleCTM(context, 1.0, -1.0); 

    // set the blend mode to color burn, and the original image 
    //CGContextSetBlendMode(context, kCGBlendModeColorBurn); 
    //CGContextSetBlendMode(context, kCGBlendModeColor); 
    CGRect rect   = CGRectMake(0, 0, image.size.width, image.size.height); 
    CGRect rectInternal = CGRectMake(3, 3, image.size.width-6, image.size.height-6); 
    //CGContextFillRect(context, rectInternal); 

    //CGColorRef topColor  = [[UIColor colorWithRed:0.0 green:5.0 blue:0.0 alpha:1.0]CGColor]; 
    //CGColorRef bottomColor = [[UIColor blackColor]CGColor]; 

    NSArray *colors = [NSArray arrayWithObjects: (id)topColor, (id)bottomColor, nil]; 
    CGFloat locations[] = {0, 1}; 


    CGGradientRef gradient = 
    CGGradientCreateWithColors(CGColorGetColorSpace(topColor), 
           (CFArrayRef)colors, locations); 

    // the start/end points 
    CGPoint top = CGPointMake(CGRectGetMidX(rectInternal), rectInternal.origin.y); 
    CGPoint bottom = CGPointMake(CGRectGetMidX(rectInternal), CGRectGetMaxY(rectInternal)); 

    // draw 
    CGContextDrawLinearGradient(context, gradient, top, bottom, 0); 

    CGGradientRelease(gradient); 


    //For fillcolors 
    //CGContextFillRect(context, rect); 

    CGContextDrawImage(context, rect, image.CGImage); 

    // set a mask that matches the shape of the image, then draw (color burn) a colored rectangle 
    //CGContextClipToMask(context, rect, image.CGImage); 
    //CGContextAddRect(context, rect); 

    //CGContextDrawPath(context,kCGPathFill); 

    // generate a new UIImage from the graphics context we drew onto 
    UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 


    //return the gradient image 
    return coloredImg; 
} 

スレッドがクローズしました。

0

おそらく少し話題ですが、UIButton classのsetImage:forState:メソッドを使用するだけで、通常のUIButtonにグラフィカルな背景を作成することができます。

+0

私はsetImageを使って問題を解決するかどうか試してみます。 – zardon

+0

@子どもの指が交差しました。 (カスタムサブビューを使用しなければならない場合よりも苦痛が少なくなります):-) –

+0

こんにちは、うまくいきますが、問題はそれが私の問題を解決せず、いくつかの文脈にそれを与える質問。 – zardon

関連する問題