Ios 使用Objective-C合并两个UIImageView,其中一个在使用transforms修改后

Ios 使用Objective-C合并两个UIImageView,其中一个在使用transforms修改后,ios,objective-c,uiimageview,Ios,Objective C,Uiimageview,我正在尝试创建一个视图控制器来混合两个UIImage。其中一个将是静态的,肌动蛋白作为背景,另一个可以缩放、拖动和旋转,以将其放置在我想要的第一个内部 我创建了一个测试视图控制器,如下所示: @interface ViewController () <UIGestureRecognizerDelegate> @property (strong, nonatomic) IBOutlet UIPanGestureRecognizer *pan; @property (strong, n

我正在尝试创建一个视图控制器来混合两个UIImage。其中一个将是静态的,肌动蛋白作为背景,另一个可以缩放、拖动和旋转,以将其放置在我想要的第一个内部

我创建了一个测试视图控制器,如下所示:

@interface ViewController () <UIGestureRecognizerDelegate>

@property (strong, nonatomic) IBOutlet UIPanGestureRecognizer *pan;
@property (strong, nonatomic) IBOutlet UIPinchGestureRecognizer *pinch;
@property (strong, nonatomic) IBOutlet UIRotationGestureRecognizer *rotation;

@end

@implementation ViewController

- (void)viewDidLoad
{
  [super viewDidLoad];

  _pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
  [_marioImageView addGestureRecognizer:_pan];

  _pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(resize:)];
  [_marioImageView addGestureRecognizer:_pinch];

  _rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
  [_marioImageView addGestureRecognizer:_rotation];
}



- (void)move:(UIPanGestureRecognizer *)pan
{
  CGPoint translation = [pan translationInView:_backImageView];

  CGPoint newCenter = CGPointMake(pan.view.center.x + translation.x,
                              pan.view.center.y + translation.y);

  CGPoint newBottomRight = CGPointMake(newCenter.x + pan.view.frame.size.width / 2,
                                     newCenter.y + pan.view.frame.size.height / 2);

  CGPoint newOrigin = CGPointMake(newCenter.x - pan.view.frame.size.width / 2,
                                newCenter.y - pan.view.frame.size.height / 2);

  if (CGRectContainsPoint(_backImageView.frame, newBottomRight) &&
    CGRectContainsPoint(_backImageView.frame, newOrigin)) {

    pan.view.center = newCenter;
    [pan setTranslation:CGPointZero inView:_backImageView];
  }
}

- (void)resize:(UIPinchGestureRecognizer *)pinch
{
  if (CGRectContainsRect(_backImageView.frame, pinch.view.frame)) {
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
    pinch.scale = 1.0;
  }
}

- (void)rotate:(UIRotationGestureRecognizer *)rotation
{
  if (CGRectContainsRect(_backImageView.frame, rotation.view.frame)) {
    rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
    rotation.rotation = 0;
  }
}

蓝色背景(
\u backImageView
)和马里奥图像(
\u marioImageView
)都是同一级别的UIImageView(没有一个是另一个的子级)。我处理所有手势识别器的方式如下:

@interface ViewController () <UIGestureRecognizerDelegate>

@property (strong, nonatomic) IBOutlet UIPanGestureRecognizer *pan;
@property (strong, nonatomic) IBOutlet UIPinchGestureRecognizer *pinch;
@property (strong, nonatomic) IBOutlet UIRotationGestureRecognizer *rotation;

@end

@implementation ViewController

- (void)viewDidLoad
{
  [super viewDidLoad];

  _pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
  [_marioImageView addGestureRecognizer:_pan];

  _pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(resize:)];
  [_marioImageView addGestureRecognizer:_pinch];

  _rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
  [_marioImageView addGestureRecognizer:_rotation];
}



- (void)move:(UIPanGestureRecognizer *)pan
{
  CGPoint translation = [pan translationInView:_backImageView];

  CGPoint newCenter = CGPointMake(pan.view.center.x + translation.x,
                              pan.view.center.y + translation.y);

  CGPoint newBottomRight = CGPointMake(newCenter.x + pan.view.frame.size.width / 2,
                                     newCenter.y + pan.view.frame.size.height / 2);

  CGPoint newOrigin = CGPointMake(newCenter.x - pan.view.frame.size.width / 2,
                                newCenter.y - pan.view.frame.size.height / 2);

  if (CGRectContainsPoint(_backImageView.frame, newBottomRight) &&
    CGRectContainsPoint(_backImageView.frame, newOrigin)) {

    pan.view.center = newCenter;
    [pan setTranslation:CGPointZero inView:_backImageView];
  }
}

- (void)resize:(UIPinchGestureRecognizer *)pinch
{
  if (CGRectContainsRect(_backImageView.frame, pinch.view.frame)) {
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
    pinch.scale = 1.0;
  }
}

- (void)rotate:(UIRotationGestureRecognizer *)rotation
{
  if (CGRectContainsRect(_backImageView.frame, rotation.view.frame)) {
    rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
    rotation.rotation = 0;
  }
}
问题是,在最终图像中,马里奥图像显示时没有任何变换(没有缩放,没有旋转,任何东西)

我做错了什么


谢谢

当您执行
imageView.image
时,它只返回
图像
,而不进行任何转换。您需要保存所有这些转换,并且需要编写自己的代码,以便从图像视图中提取转换后的图像

一种方法是在旋转/缩放/变换后截屏图像视图:

    //call this method after the rotation/scaling is done i.e in your mix method
    -(UIImage *)screenshotImageView :(UIImageView *)imgV{

    UIGraphicsBeginImageContext(imgV.frame.size);

    CGContextRef c = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(c, imgV.frame.origin.x, imgV.frame.origin.y);

    [self.view.layer renderInContext:c];
       image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;

    }
因此,在本例中,要合并的两个图像方法是:

    UIImage *backImage = [self screenshotImageView:_backImageView];
    UIImage *marioImage =[self screenshotImageView:_marioImageView];
使用返回的图像按需合并图像

另一种方法是基于imageproperties获取变换后的图像:

假设您在imageView中翻转了图像,如果要获取翻转的图像:

您可以尝试:

     UIImage* flippedImage = [UIImage imageWithCGImage:imgView.image.CGImage
                                                scale:imgView.image.scale
                                          orientation:UIImageOrientationUpMirrored];