gpt4 book ai didi

iphone - 如何旋转 CGRect 或 CGImageCreateWithImageInRect?

转载 作者:行者123 更新时间:2023-11-28 19:21:00 26 4
gpt4 key购买 nike

描述我的项目:

我有一个矩形 UIImageView 框架漂浮在白色层上。在 UIImageView 内部,我成功地创造了它在白色层后面显示背景图像的一部分的错觉。您可以四处拖动矩形,它会“重新绘制”图像,以便您可以看到白色后面的内容。它基本上是这段代码:

//whenever the frame is moved, updated the CGRect frameRect and run this:

self.newCroppedImage = CGImageCreateWithImageInRect([bgImage.image CGImage], frameRect);
frame.image = [UIImage imageWithCGImage:self.newCroppedImage];

无论如何,我还有一个旋转手势识别器,允许用户旋转框架(并因此旋转图像)。这是因为发送到 CGImageCreateWithImageInRect 的 CGRect 仍然以其原始旋转为导向。这打破了您正在透过 window 观看的错觉,因为您看到的图像是旋转的,而只有框架应该以这种方式出现。

理想情况下,我需要旋转我的框架并将其应用于从我的 bgImage 创建的图像。有没有人对我如何应用它有任何线索或想法?

最佳答案

我建议您采用不同的方法。不要不断创建新图像以放入您的 UIImageView。相反,像这样设置 View 层次结构:

White view
"Hole" view (just a regular UIView)
Image view

也就是说,白色 View 将孔 View 作为 subview 。孔 View 将 UIImageView 作为其 subview 。

孔 View 必须将其 clipsToBounds 属性设置为 YES(您可以使用代码或在您的 Nib 中设置它)。

ImageView 的大小应设置为其图像的大小。这当然会大于孔 View 的大小。

这非常非常重要: ImageView 的中心必须设置为孔 View 的中心。

这是我在测试项目中用于设置的代码。白色 View 是 self.view。我从以白色 View 为中心的孔开始,并将 ImageView 的中心设置为孔 View 的中心。

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];

CGRect bounds = self.view.bounds;
self.holeView.center = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
self.holeView.clipsToBounds = YES;

bounds = self.holeView.bounds;
self.imageView.center = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
self.imageView.bounds = (CGRect){ CGPointZero, self.imageView.image.size };

}

我还将 ImageView 的大小设置为其图像的大小。您可能希望将其设置为白色 View 的大小。

要平移和旋转孔,我将设置 holeView.transform。我不会更改 holeView.frameholeView.center。我有两个实例变量,_holeOffset_holeRotation,我用它们来计算变换。使它看起来像白色 View 中的一个洞,显示 ImageView 的技巧是对 ImageView 应用反向变换,取消孔 View 变换的效果:

- (void)updateTransforms {
CGAffineTransform holeTransform = CGAffineTransformIdentity;
holeTransform = CGAffineTransformTranslate(holeTransform, _holeOffset.x, _holeOffset.y);
holeTransform = CGAffineTransformRotate(holeTransform, _holeRotation);
self.holeView.transform = holeTransform;
self.imageView.transform = CGAffineTransformInvert(holeTransform);
}

这种在 subview 上使用逆变换的技巧只有在 subview 的中心位于其父 View 的中心时才有效。 (从技术上讲, anchor 必须对齐,但默认情况下, View 的 anchor 位于其中心。)

我在 holeView 上放了一个 UIPanGestureRecognizer。我将其配置为将 panGesture: 发送到我的 View Controller :

- (IBAction)panGesture:(UIPanGestureRecognizer *)sender {
CGPoint offset = [sender translationInView:self.view];
[sender setTranslation:CGPointZero inView:self.view];
_holeOffset.x += offset.x;
_holeOffset.y += offset.y;
[self updateTransforms];
}

我还在 holeView 上放了一个 UIRotationGestureRecognizer。我将其配置为发送 rotationGesture: 到我的 View Controller :

- (IBAction)rotationGesture:(UIRotationGestureRecognizer *)sender {
_holeRotation += sender.rotation;
sender.rotation = 0;
[self updateTransforms];
}

关于iphone - 如何旋转 CGRect 或 CGImageCreateWithImageInRect?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8868195/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com