/*
UIImageの合成
http://iphone-dev.g.hatena.ne.jp/horigood/20090702/1246547603
*/
//バンドル画像からUIImageを2つ作成
UIImage *imgA = [UIImage iamgeNamed:@"hogeA"];
UIImage *imgB = [UIImage iamgeNamed:@"hogeB"];
int widthA = CGImageGetWidth(imgA.CGImage);
int widthB = CGImageGetWidth(imgB.CGImage);
int height = CGImageGetHeight(imgA.CGImage);
//CGContextを作成
int widthC = widthA + widthB;
unsigned char *bitmap = malloc(widthC * height * sizeof(unsigned char) * 4);
CGContextRef bitmapContext = CGBitmapContextCreate(bitmap,
width,
height,
8,
width * 4,
CGColorSpaceCreateDeviceRGB(),
kCGImageAlphaPremultipliedFirst);
//imgAをbitmapContextに描画
CGContextDrawImage(bitmapContext, CGRectMake(0, 0, widthA, height), imgA.CGImage);
//imgBをimgAの隣に描画
CGContextDrawImage(bitmapContext, CGRectMake(widthA, 0, widthB, height), imgB.CGImage);
//CGContextからCGImageを作成
CGImageRef imgRef = CGBitmapContextCreateImage (bitmapContext);
//CGImageからUIImageを作成
UIImage *imgC = [UIImage imageWithCGImage:imgRef];
//bitmapを解放(paellaさんのコメントを受けて追記)
free(bitmap);
最終更新:2012年09月11日 09:13