2011-05-28 109 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