使用图形上下文合成:将两张图片绘画到上下文中,在从上下文获取:
- (UIImage*)composeImage:(UIImage*)backImg andFrontImg:(UIImage*)frontImg {
CGSize imgSize = backImg.size;
UIGraphicsBeginImageContext(imgSize); //创建图形上下文
[backImg drawInRect:CGRectMake(0, 0, imgSize.width, imgSize.height)]; //将图backImg绘画到上下文
imgSize = frontImg.size;
[frontImg drawInRect:CGRectMake(0, 0, imgSize.width, imgSize.height)]; //将图frontImg绘画到图形上下文上
UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext(); //从上下文获取绘画好的图片
UIGraphicsEndImageContext(); //释放上下文
return resultImg;
}
CGSize imgSize = backImg.size;
UIGraphicsBeginImageContext(imgSize); //创建图形上下文
[backImg drawInRect:CGRectMake(0, 0, imgSize.width, imgSize.height)]; //将图backImg绘画到上下文
imgSize = frontImg.size;
[frontImg drawInRect:CGRectMake(0, 0, imgSize.width, imgSize.height)]; //将图frontImg绘画到图形上下文上
UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext(); //从上下文获取绘画好的图片
UIGraphicsEndImageContext(); //释放上下文
return resultImg;
}