旋转图像后如何合并两个图像?

时间:2012-09-13 05:15:57

标签: iphone

enter image description here

旋转图像后如何合并旋转图像?我使用下面的代码,它在图像旋转之前工作正常。如何解决这个问题。请帮我。提前谢谢。

CGRect backgroundImageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
CGRect foregroundImageRect = CGRectMake(rsImageView.frame.origin.x, rsImageView.frame.origin.y, rsImageView.frame.size.width, rsImageView.frame.size.height);

[backgroundImageView.image drawInRect:backgroundImageRect];
[rsImageView.image drawInRect:foregroundImageRect];
overlappedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

2 个答案:

答案 0 :(得分:1)

添加此方法在轮换后调用此方法:

-(UIImage *)getMergedImage
{
 UIImage mergedImage;
 UIGraphicsBeginImageContext(backgroundImageView.frame.size)
 [backgroundImageView.layer  renderInContext:UIGraphicsGetCurrentContext];//bacgroundImageView here
 UIImage overLappedImage = [self getOverlappedImage];
 if(overLappedImage)
 {
   //CGRectMake(0,0,rsImageView.frame.width,rsImageView.frame.height)
   [overLappedImage drawInRect:rsImageView.frame blendMode:kCGBlendModeNormal alpha:0.8];//TopMostImageView here
 }
 mergedImage = UIGraphicsGetImageFromCurrentImageContext(); 
 UIGraphicsEndImageContext();
 return mergedImage;
}

添加此方法以获得重叠的图像。

-(UIImage *)getOverlappedImage
{
  UIImage overlappedImage;
  UIGraphicsBeginImageContext(rsImageView.frame.size)
  [rsImageView.layer renderInContext:UIGraphicsGetCurrentContext]; //TopMostImageView here
  overlappedImage = UIGraphicsGetImageFromCurrentImageContext(); 
  UIGraphicsEndImageContext();
  return overlappedImage;
}

答案 1 :(得分:0)

如果你想在设备方向时使用此代码

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
    toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
    //Your cgrectmake for landscape
}
else
{
    //Your cgrectmake for Portrait
}
}