我在使用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];
}
答案 0 :(得分: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屏幕有关。虽然,我没有在非视网膜屏幕设备上测试过这个。