2

我正在尝试创建一个允许用户在图像上移动框架的应用程序,以便我可以在选定区域上应用一些效果。

我需要允许用户精确地拖动和缩放图像上的蒙版框架。我需要准确地说,就像任何其他照片应用程序一样。

我的策略是在触摸移动事件上获取用户的接触点,并相应地缩放我的框架。那很直观。我编写了以下代码来处理触摸移动事件:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:[self view]];


    float currX = touchPoint.x;
    float currY = touchPoint.y;

    /*proceed with other operations on currX and currY, 
      which is coming out quite well*/

}

但唯一的问题是,currXcurrY变量的坐标并不完全在它们应该在的位置。存在视差错误,不断从设备转移到设备。我还认为在 iPad 的情况下 x 和 y 坐标会被交换。

你能帮我弄清楚如何获得确切的触摸坐标吗?

我的背景图像在一个视图 ( imageBG) 中,而蒙版框架在一个单独的视图 ( maskBG) 中。我试过了:

CGPoint touchPoint = [touch locationInView:[maskBG view]];

CGPoint touchPoint = [touch locationInView:[imageBG view]];

...但同样的问题仍然存在。我还注意到 iPad 上的触摸错误比 iPhone 或 iPod 上的更严重。

4

3 回答 3

1

image.center = [[[event allTouches] anyObject] locationInView:self.view];

于 2013-07-10T11:05:52.223 回答
0

你试过这些:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:selectedImageView];

float currX = (touchPoint.x)/selectedImageView.frame.size.width;
float currY = (touchPoint.y)/selectedImageView.frame.size.height;

/*proceed with other operations on currX and currY,
 which is coming out quite well*/
}

或者你也可以使用 UIPanGestureRecognizer..

于 2015-04-01T12:47:07.740 回答
0

您好,您的问题是图像和 iPhone 屏幕的宽高比不一定相同。您的触摸点可能无法正确转换为您的实际图像。

- (UIImage*) getCroppedImage {
    CGRect rect = self.movingView.frame;
    CGPoint  a;
    a.x=rect.origin.x-self.imageView.frame.origin.x;
    a.y=rect.origin.y-self.imageView.frame.origin.y;

    a.x=a.x*(self.imageView.image.size.width/self.imageView.frame.size.width);
    a.y=a.y*(self.imageView.image.size.height/self.imageView.frame.size.height);

    rect.origin=a;
    rect.size.width=rect.size.width*(self.imageView.image.size.width/self.imageView.frame.size.width);
    rect.size.height=rect.size.height*(self.imageView.image.size.height/self.imageView.frame.size.height);

    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    // translated rectangle for drawing sub image 
    CGRect drawRect = CGRectMake(-rect.origin.x, -rect.origin.y, self.imageView.image.size.width, self.imageView.image.size.height);

    // clip to the bounds of the image context
    // not strictly necessary as it will get clipped anyway?
    CGContextClipToRect(context, CGRectMake(0, 0, rect.size.width, rect.size.height));

    // draw image
    [self.imageView.image drawInRect:drawRect];

    // grab image
    UIImage* croppedImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return croppedImage;
}

这就是我为裁剪移动视图所做的操作,这是我为裁剪传递的矩形,看看它是如何被翻译以正确反映在图像上的。确保用户看到图像的图像视图是 aspectfit 内容模式。

注意:-我使图像视图的矩形适合 aspectFit 图像

用这个来做

- (CGSize)makeSize:(CGSize)originalSize fitInSize:(CGSize)boxSize
{
    widthScale = 0;
    heightScale = 0;

    widthScale = boxSize.width/originalSize.width;
    heightScale = boxSize.height/originalSize.height;

    float scale = MIN(widthScale, heightScale);

    CGSize newSize = CGSizeMake(originalSize.width * scale, originalSize.height * scale);

    return newSize;
}
于 2013-02-25T06:17:01.747 回答