2012-09-27 69 views
2

我有兩個UIImage的實例。我如何創建第三個UIImage這只是將兩張原始圖像拼接在一起?我希望第一個圖像在頂部,第二個圖像在底部,這樣頂部圖像的底部邊緣與底部圖像的頂部邊緣齊平。從兩個較小的UIImages創建UIImage

回答

6

這樣的事情應該工作(我還沒有測試,但它)

-(UIImage *)imageWithTopImage:(UIImage *)topImage bottomImage:(UIImage *)bottomImage 
{ 
    UIGraphicsBeginImageContext(CGSizeMake(topImage.size.width, topImage.size.height + bottomImage.size.height); 
    [topImage drawInRect:CGRectMake(0, 0, topImage.size.width, topImage.size.height)]; 
    [bottomImage drawInRect:CGRectMake(0, topImage.size.width, bottomImage.size.width, bottomImage.size.height)]; 
    UIImage *combinedImage = UIGraphicsGetImageFromCurrentImageContext();  
    UIGraphicsEndImageContext(); 
    return combinedImage; 
} 
+0

我認爲CGSizeMake在第一行缺少一個括號。 – Cindeselia

+0

此外,bottomImage drawInRect調用可能應該從topImage.size.height的y座標開始,而不是其寬度。 – Cindeselia