0

我正在创建一个提供自定义图像裁剪的应用程序…… 其中用户使用矩形选择图像的一部分,该矩形应调整大小并可以根据用户触摸移动以裁剪该部分……</p>

我想要图像上的矩形,以便我可以使用以下方法裁剪该部分

我可以通过以下方法在图像上裁剪矩形

-(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];

}

我想要绘制矩形的解决方案,可以通过触摸调整大小并通过触摸移动...

我是 iPhone 的新手,所以....请帮助我如何做到这一点....

4

2 回答 2

2

我从这个链接Image Cropper的好例子中得到了答案。

如果有人有比这更好的,请把它放在这里....如果值得的话,我会接受这个答案

于 2012-10-03T05:18:18.663 回答
0

这是使用UILongGesture2 个手指的简单方法。选择矩形仅限于触摸。SelectionViewUIView最初是隐藏的。

这是手势的初始化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;
}
于 2012-08-05T11:42:56.093 回答