图像裁剪为矩形,它应该移动
Posted
技术标签:
【中文标题】图像裁剪为矩形,它应该移动【英文标题】:Image Cropping in rectangle shape and it shud move 【发布时间】:2014-07-30 11:35:12 【问题描述】:我想裁剪一个矩形大小的 UIImageView,它应该可以调整大小,请任何人帮助我,我很挣扎。
【问题讨论】:
【参考方案1】:您可以使用下面的代码添加平移手势,您可以从中移动图像视图和捏合手势,您可以通过该手势调整图像大小
-(void)addGesture
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[imgView addGestureRecognizer:panRecognizer];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(resize:)];
[imgView addGestureRecognizer:pinchRecognizer];
-(void)move:(id)sender
[self.view bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
if ([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan)
firstX = [[sender view] center].x;
firstY = [[sender view] center].y;
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY);
[[sender view] setCenter:translatedPoint];
- (void)resize:(UIPinchGestureRecognizer *)recognizer
if([recognizer state] == UIGestureRecognizerStateBegan)
_lastScale = 1.0;
if ([recognizer numberOfTouches] >= 2) //should always be true when using a PinchGR
CGPoint touch1 = [recognizer locationOfTouch:0 inView:self.imageForEditing];
CGPoint touch2 = [recognizer locationOfTouch:1 inView:self.imageForEditing];
CGPoint mid;
mid.x = ((touch2.x - touch1.x) / 2) + touch1.x;
mid.y = ((touch2.y - touch1.y) / 2) + touch1.y;
CGSize imageViewSize = self.imageForEditing.frame.size;
CGPoint anchor;
anchor.x = mid.x / imageViewSize.width;
anchor.y = mid.y / imageViewSize.height;
self.imageForEditing.layer.anchorPoint = anchor;
CGFloat scale = 1.0 - (_lastScale - [recognizer scale]);
CGAffineTransform currentTransform = self.imageForEditing.transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[self.imageForEditing setTransform:newTransform];
_lastScale = [recognizer scale];
【讨论】:
我需要裁剪图像并且叠加层应该可以调整大小和移动 您可以使用下面的代码: // 返回调整大小为所需 CGSize 的图像 - (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize UIGraphicsBeginImageContext(newSize); [图像 drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();返回新图像; 我希望它可以通过触摸移动,请给我发送代码 iam 初学者请帮助我 谢谢它的工作,但我希望它通过移动图像的角落来调整大小 然后您需要在 UIImageView 的每个角落添加四个小的自定义视图并以编程方式处理所有视图的触摸就像您尝试使用右上角视图缩放图像一样,您只需要增加/减少原点 x & y 并根据比例增加/减少宽度和高度,但许多代码可用于此以上是关于图像裁剪为矩形,它应该移动的主要内容,如果未能解决你的问题,请参考以下文章