如果您使用 UIView 包含更多要合并的 2 个图像,您可以尝试将视图捕获为要使用的图像。
//捕获视图到图像
-(UIImage *)captureImageFromView
{
UIView *theView = self; //自扩展UIView
如果(空!= UIGraphicsBeginImageContextWithOptions)
{
UIGraphicsBeginImageContextWithOptions(theView.bounds.size, NO, 2.0f);
}
别的
{
//iOS 4.0
UIGraphicsBeginImageContext(theView.frame.size);
}
[theView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *captureImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
返回捕获图像;
}
//将2个图像合并为一个图像。
-(UIImage *)mergeBaseImage:(UIImage *)_baseImage
underImage:(UIImage *)_underImage
matchBaseImageSize:(BOOL)_matchBaseImageSize
{
UIGraphicsBeginImageContext(_baseImage.size);
[_baseImage drawInRect:CGRectMake(0, 0, _baseImage.size.width, _baseImage.size.height)];
如果(_matchBaseImageSize)
{
[_underImage drawInRect:CGRectMake(0, 0, _baseImage.size.width, _baseImage.size.height)];
}
别的
{
[_underImage drawInRect:CGRectMake(0, 0, _underImage.size.width, _underImage.size.height)];
}
UIImage *_mergedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
返回_mergedImage;
}
希望它可以帮助你。