2011-01-05 68 views
0

我遇到以下代码的问题。为什么我不能在一个以上的按钮上使用一个UIImageView?

我想在一个以上的按钮上使用一个UIImageView,对我来说,只定义一次UIImageView并根据需要多次使用它似乎是符合逻辑的。

如果我尝试将imgView1附加到两个不同的按钮,则只显示其中一个按钮。

我可以通过使用注释掉的imgView2来解决这个问题。我不得不这样做似乎很愚蠢。为什么我不能只有一个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]; 

编辑:

语境

我已经添加了一些背景,以我的问题

我试图创建,坐在后面的实际渐变背景层图片图像,并且是这一个的后续问题(网址:Making a Transparent image button with bgcolor

Whil我可以简单地使用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. 梯度的背景是在背景和被迫进入frame
  3. 整个对象是可点击
  4. 我应该只需要初始化1周的UIImageView每次我想使用它,但我被迫创建相同的UIImageView的多个副本。

我希望这能够澄清我的问题的背景。

回答

0

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

上面的网址帮我,现在解决这个问题。

-(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

或许有点题外话,但你一定要明白,你可以通过简单地用setImage创建一个正常的UIButton图形背景:forState:该UIButton class的方法。

+0

我会尝试使用setImage,看看它是否可以解决问题。 – zardon 2011-01-05 17:29:14

+0

@zardon手指交叉。 (如果它使用自定义的子视图,那么它的痛苦会少得多):-) – 2011-01-05 17:32:38

+0

你好,是的,它工作正常 - 但问题是,它不能解决我的问题,并延长了我的问题给它一些上下文。 – zardon 2011-01-05 17:46:55

相关问题