使用图形上下文合成:将两张图片绘画到上下文中,在从上下文获取:
- (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;
}
本文介绍了一种使用图形上下文合成两张图片的方法。首先创建图形上下文,然后将背景图片和前景图片绘制到上下文中,最后从上下文中获取合成后的图片。
209

被折叠的 条评论
为什么被折叠?



