2

我在使用 GPUImage 变换过滤器时遇到问题。我正在使用平移手势识别器来重新定位图像。我的代码有效,但图像以大约一半的速度移动。如果我CGAffineTransform newTransform将坐标加倍,图像会按预期拖动。但是,当我开始一个新的平移手势时,图像会跳到距离中心大约两倍的点。也许我的数学是错误的。有任何想法吗?或者,任何人都可以提出比我这里更好的解决方案吗?

- (void)move:(UIPanGestureRecognizer *)sender {
    // Translated CGPoint from GPUImageView
    CGPoint translation = [sender translationInView:self.primaryImageView];
    // Current transform from GPUImageTransformFilter
    CGAffineTransform currentTransform = self.transFilter.affineTransform;
    // Size of GPUImageView bounds for later calculations
    CGSize size = self.primaryImageView.bounds.size;

    if ([sender state] == UIGestureRecognizerStateBegan) {
        // Set a beginning CGPoint 
        // Multiply GPUImageView bounds by current transform to get
        // the translated coordinates in pixels.
        self.beginPoint = CGPointMake(size.width*currentTransform.tx, size.height*currentTransform.ty);
    }

    // Calculate difference from beginning point to translated point
    CGPoint updatedPoint = CGPointMake(self.beginPoint.x+translation.x, self.beginPoint.y+translation.y);

    // Create a new transform translation.
    // Divide updated coordinates by GPUImageView bounds to get
    // a percentage value (-1 to 1)
    CGAffineTransform newTransform = CGAffineTransformMakeTranslation(updatedPoint.x/(size.width), updatedPoint.y/(size.height));

    // Apply new transform to filter and process.
    [self.transFilter setAffineTransform:newTransform];
    [self.sourcePicture processImage];
}
4

1 回答 1

2

正如@BradLarson 建议的那样,我创建了一个使用CGAffineTransformTranslate(). 我还发现平移计算还必须考虑变换比例以准确平移位置。这是我的解决方案:

- (void)move:(UIPanGestureRecognizer *)sender {
    CGPoint translatedPoint = [sender translationInView:self.primaryImageView];
    if ([sender state] == UIGestureRecognizerStateBegan) {
        self.lastPoint = translatedPoint;
    }

    CGSize size = self.primaryImageView.bounds.size;
    // Subtract the last point from the translated point to get the difference.
    CGPoint updatedPoint = CGPointMake(translatedPoint.x-self.lastPoint.x, translatedPoint.y-self.lastPoint.y);
    CGAffineTransform currentTransform = self.transFilter.affineTransform;
    // Divide updated point by the bounds to get the transform translate value.
    // Multiply transform value by the result of the offset factor divided
    // by the transform scale value.
    CGAffineTransform newTransform = CGAffineTransformTranslate(currentTransform, (updatedPoint.x/size.width)*(2/currentTransform.a), (updatedPoint.y/size.height)*(2/currentTransform.a));

    [self.transFilter setAffineTransform:newTransform];
    [self.sourcePicture processImage];
    self.lastPoint = translatedPoint;
}

我已将偏移因子设置为2. 我仍然不确定为什么这个偏移是必要的,但我猜它可能与 Retina 屏幕有关。虽然,我还没有在非视网膜屏幕设备上测试过。

于 2013-03-01T17:43:58.063 回答