将图像视图分配给按钮的背景图像

时间:2011-05-28 19:23:40

标签: iphone

我正在尝试将两个图像视图组合在一起并将它们放入另一个图像视图中,我将其分配给按钮的背景图像。这样做不起作用。有人可以检查一下,让我知道这里到底出了什么问题。

    UIImageView * image1 = [[UIImageView alloc] initWithImage:[UIImage    imageNamed:@"homesmall.png"]];
    UIImageView * image2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"update.png"]];

    [image1 setFrame:CGRectMake(16,0,16,16)];
    [image2 setFrame:CGRectMake(0,0,16,16)];

    UIImageView *cellAcc = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,32,16)];

    [cellAcc addSubview:image1];
    [cellAcc addSubview:image2];
    UIImage *image = [cellAcc image];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
    button.frame = frame;
    [button setBackgroundImage:image forState:UIControlStateNormal];

1 个答案:

答案 0 :(得分:0)

您要将它们添加为子视图。这里没有添加剂属性。您将不得不使用石英将它们绘制到图像上下文中并提取组合图像。

修改

您可以替换

UIImage *image = [cellAcc image];

UIGraphicsBeginImageContext(cellAcc.bounds.size);
[cellAcc.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();