菜鸟教程小白 发表于 2022-12-11 18:33:37

ios - 将 UIScrollViewPoint 转换为 UIImage + ZOOM + ROTATE


                                            <p><p>我需要裁剪加载在 <code>UIScrollview</code> 中的 <code>UIImage</code> 以及另一个 <code>UIView</code> 的矩形,该矩形也在 <code>UIScrollView</code></p>

<p>下面是 View 层次结构</p>

<pre><code> --&gt; View
   --&gt;UIScrollView
      --&gt; viewBase (UIView)
             --&gt; UIImageView -&gt; (Zoomed &amp; rotated )
             --&gt; UIView (Target View)(Movable User can move anywhere in scrollviewto crop rect)
</code></pre>

<p>我的图像被旋转和缩放我需要在 <code>TargetView</code></p> 中获取图像的确切部分

<p>我正在绘制 <code>UIImage</code> 并在 <code>context</code> 上旋转以下是代码</p>

<pre><code>CGFloat angleCroppedImageRetreacted = atan2f(self.imgVPhoto.transform.b, self.imgVPhoto.transform.a);
angleCroppedImageRetreacted = angleCroppedImageRetreacted * (180 / M_PI);

UIView *rotatedViewBox = [ initWithFrame:CGRectMake(0.0f, 0.0f, self.imgVPhoto.image.size.width, self.imgVPhoto.image.size.height)];
rotatedViewBox.transform = CGAffineTransformMakeRotation(-angleCroppedImageRetreacted);
CGSize rotatedSize = rotatedViewBox.frame.size;

UIGraphicsBeginImageContext(rotatedSize);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(bitmap, rotatedSize.width / 2.0f, rotatedSize.height / 2.0f);
CGContextRotateCTM(bitmap, -angleCroppedImageRetreacted);
CGContextScaleCTM(bitmap, 1.0f, -1.0f);



CGContextDrawImage(bitmap, CGRectMake(-self.imgVPhoto.image.size.width / 2.0f,
                                    -self.imgVPhoto.image.size.height / 2.0f,
                                    self.imgVPhoto.image.size.width,
                                    self.imgVPhoto.image.size.height),
                   self.imgVPhoto.image.CGImage);
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();




UIGraphicsEndImageContext();
</code></pre>

<p>而且效果很好。我得到的旋转 UIImage 与我在模拟器中看到的一样</p>

<p>对于 <strong>converting</strong> 目标 View 点到 <code>UIImage</code> 我使用以下代码,它不工作</p>

<pre><code> CGPoint imageViewPoint = ;

float percentX = imageViewPoint.x / self.imgVPhoto.frame.size.width;
float percentY = imageViewPoint.y / self.imgVPhoto.frame.size.height;

CGPoint imagePoint = CGPointMake(resultImage.size.width * percentX, resultImage.size.height * percentY);

rect.origin = imagePoint;

//rect.origin.x *= (self.imgVPhoto.image.size.width / self.imgVPhoto.frame.size.width);
//rect.origin.y *= (self.imgVPhoto.image.size.height / self.imgVPhoto.frame.size.height);


imageRef = CGImageCreateWithImageInRect(, rect);
img = ;
</code></pre>

<p>我认为问题是我们不能在变换应用后使用 <code>Rect</code> </p>

<p>请帮我裁剪 <code>UIImage</code>,它是从同一层次结构上的 rect 缩放和旋转的</p>

<p>如果您需要更多信息,请询问</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我正在回答我自己的问题。 </p>

<p>感谢 Matic 提供的想法</p>

<p>我改变了一个逻辑</p>

<p>我已经实现了我正在寻找的相同功能</p>

<pre><code>CGPoint locationInImageView =; // received from touch
locationInImageView =;

// I GOT LOCATION IN UIIMAGEVIEW OF TOUCH POINT

UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0);

;

UIImage *img1 = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

// I GOT UIIMAGE FROMCURRENT CONTEXT

CGFloat width = self.targetImageview.frame.size.width * self.zoomScale ;
CGFloat height = self.targetImageview.frame.size.height * self.zoomScale ;

//2 IS SCALE FACTOR

CGFloat xPos = (locationInImageView.x* 2)- width / 2;
CGFloat yPos = (locationInImageView.y * 2) - height / 2;

CGRect rect1 = CGRectMake(xPos, yPos, width, height);

CGImageRef imageRef = CGImageCreateWithImageInRect(, rect1);


// YAHHH YOU HAVE EXACT IMAGE
UIImage *img = ;
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 将 UIScrollViewPoint 转换为 UIImage &#43; ZOOM &#43; ROTATE,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/45854149/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/45854149/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 将 UIScrollViewPoint 转换为 UIImage &#43; ZOOM &#43; ROTATE