我正在创建一个提供自定义图像裁剪的应用程序.... 其中用户使用矩形来选择图像的一部分,该矩形应该被重新调整大小。可以根据用户触摸移动以裁剪该部分......
我想在图像上使用rect,以便我可以使用以下方法裁剪该部分
我可以通过以下方法
在图像上裁剪矩形-(void) cropImage
{
CGSize size = iv.frame.size;
CGRect rect = CGRectMake(size.width / 4, size.height / 4, size.width/2,size.height / 2);
CGImageRef imageRef = CGImageCreateWithImageInRect([iv.image CGImage], rect);
UIImage *img = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
// Display Image
UIImageView *imageView = [[UIImageView alloc] initWithImage:img];
[imageView setFrame:CGRectMake(70.0, 480.0,(size.width / 2), (size.height / 2))];
[scrollView addSubview:imageView];
[imageView release];
[scrollView scrollRectToVisible:imageView.frame animated:YES];
}
我想要绘制rect的解决方案,可以通过触摸和放大来调整大小。被触摸感动......
我是iPhone的新手....请帮助我如何做到这一点......
答案 0 :(得分:2)
我从这个链接Image Cropper ...
的好例子中得到了答案如果有人比这更好,请把它放在这里......如果它值得我接受那个答案
答案 1 :(得分:0)
这是一种使用带有2个手指的UILongGesture
的简单方法。选择矩形仅限于触摸。 SelectionView最初是隐藏的UIView
。
这是UIViewController
:
UILongPressGestureRecognizer *twoFingerLongGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleTwoFingerLongGesture:)];
[twoFingerLongGesture setNumberOfTouchesRequired:2];
[self.view addGestureRecognizer:twoFingerLongGesture];
这是手势的处理:
- (void)handleTwoFingerLongGesture:(UIGestureRecognizer *)sender
{
switch (sender.state) {
case UIGestureRecognizerStateBegan:
{
NSLog(@"mainView 2FingerLONG - UIGestureRecognizerStateBegan");
CGPoint p1 = [sender locationOfTouch:0 inView:self.view];
CGPoint p2 = [sender locationOfTouch:1 inView:self.view];
[self updateFrameSelectionWithPoint1:p1 point2:p2];
[self.view bringSubviewToFront:selectionView];
selectionView.hidden = NO;
break;
}
case UIGestureRecognizerStateChanged:
{
NSLog(@"mainView 2FingerLONG - UIGestureRecognizerStateChanged");
CGPoint p1 = [sender locationOfTouch:0 inView:self.view];
CGPoint p2 = [sender locationOfTouch:1 inView:self.view];
[self updateFrameSelectionWithPoint1:p1 point2:p2];
break;
}
case UIGestureRecognizerStateEnded:
{
selectionView.hidden = YES;
// Here you must perform the selection
break;
}
case UIGestureRecognizerStateCancelled:
{
selectionView.hidden = YES;
break;
}
default:
break;
}
}
- (void) updateFrameSelectionWithPoint1:(CGPoint)p1 point2:(CGPoint)p2
{
CGRect frameSelection;
frameSelection.origin.x = fminf(p1.x, p2.x);
frameSelection.origin.y = fminf(p1.y, p2.y);
frameSelection.size.width = fabsf(p1.x - p2.x);
frameSelection.size.height = fabsf(p1.y - p2.y);
selectionView.frame = frameSelection;
}