简体   繁体   English

通过手势识别器操作后裁剪UIImageView

[英]Crop UIImageView after manipulated through gesture recognizer

I'm having issues figuring this out! 我在解决这个问题时遇到了问题! All of the examples I have seen have to do with a scrollView and I am not using one. 我看到的所有示例都与scrollView有关,而我没有使用其中的一个。 I need to crop an image within a predetermined CGRect area after the image has been manipulated through pinch, pan, and rotate. 通过捏,平移和旋转操作图像后,我需要在预定的CGRect区域内裁剪图像。 The code I have below crops the unmanipulated image in the upper left hand corner. 我下面的代码在左上角裁剪未处理的图像。

To Clarify pendantCanvasView is the view (container) and pendantImageView is a subclass of pendantCanvasView. 要澄清,permanentCanvasView是视图(容器),而permanentImageView是enhantCanvasView的子类。 PendantFrame is just a CGRect that has the coords of the rect in pendantImageView I want to crop. PendantFrame只是一个CGRect,在我要裁剪的hangerImageView中具有rect的坐标。

Can someone help me? 有人能帮我吗?

Here is the code i have so far: 这是我到目前为止的代码:

- (void)addMoveImageToolbox {

   UIPinchGestureRecognizer *pinchRec = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)];
   [self.pendantCanvasView addGestureRecognizer:pinchRec];

   UIRotationGestureRecognizer *rotateRec = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotate:)];
   [self.pendantCanvasView addGestureRecognizer:rotateRec];

   UIPanGestureRecognizer *panRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
   [self.pendantCanvasView addGestureRecognizer:panRec];
}

- (void)handlePinch:(UIPinchGestureRecognizer*)pinch {
   self.pendantImageView.transform = CGAffineTransformScale(self.pendantImageView.transform, pinch.scale, pinch.scale);
   self.zoomScale = pinch.scale;
   pinch.scale = 1;
}

- (void)handleRotate:(UIRotationGestureRecognizer*)rotate {
   self.pendantImageView.transform =   CGAffineTransformRotate(self.pendantImageView.transform, rotate.rotation);
   rotate.rotation = 0;
}

- (void)handlePan:(UIPanGestureRecognizer *)pan {

   CGPoint translation = [pan translationInView:self.pendantCanvasView];
   self.pendantImageView.center = CGPointMake(self.pendantImageView.center.x + translation.x,
                                     self.pendantImageView.center.y + translation.y);
   [pan setTranslation:CGPointMake(0, 0) inView:self.pendantImageView];
}

Crop Method: 裁切方法:

- (UIImage *)captureScreenInRect {

   UIGraphicsBeginImageContextWithOptions(pendantFrame.size, NO, [UIScreen mainScreen].scale);
   [self.pendantCanvasView drawViewHierarchyInRect:pendantFrame afterScreenUpdates:YES];
   UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();self.pendantImageView.constraints

   return image;
}

I believe drawViewHierarchyInRect re-renders the view hierarchy so it may be losing your alterations to the imageView. 我相信drawViewHierarchyInRect会重新渲染视图层次结构,因此它可能会丢失对imageView的更改。

you could try using snapshotViewAfterScreenUpdates instead. 您可以尝试改用snapshotViewAfterScreenUpdates。

I'm not sure what the relationships between pendantCanvasView, pendantImageView, and pendantFrame are but it may be possible to use the renderInContext method of the layer property of one or the other to get the cropped image. 我不确定hookerCanvasView,permanentImageView和wingsFrame之间的关系是什么,但是可以使用一个或另一个的layer属性的renderInContext方法来获取裁剪的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM