2011-05-28 69 views
0

我想结合两个图像视图,并把它们放到另一个图像视图,我将其分配给一个按钮的背景图像。总之,这是行不通的。有人请检查并让我知道这里到底是什么错误。分配图像视图到背景图像的按钮

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

回答

0

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

编辑

可以更换

UIImage *image = [cellAcc image]; 

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

我尝试这样做,这也适用 CGSize itemSize = CGSizeMake(32,16); \t \t UIGraphicsBeginImageContext(itemSize); \t \t CGRect imageRect = CGRectMake(16,0.0,16,16); \t \t [[UIImage imageNamed:@“homesmall.png”] drawInRect:imageRect]; \t \t CGRect imageRect1 = CGRectMake(0.0,0.0,16,16); \t \t [[UIImage imageNamed:@“update.png”] drawInRect:imageRect1]; UIImageImage = UIGraphicsGetImageFromCurrentImageContext();我们可以通过下面的方法来创建一个UIImage对象。 – agupta 2011-05-28 19:53:31

+0

更清洁的方法! – 2011-05-28 19:55:03