菜鸟教程小白 发表于 2022-12-13 16:05:18

ios - 用路径提取 UIImage


                                            <p><p>如何提取带有路径的 UIImage? </p>

<p>我需要将路径内的内容放入新的 UIImage。我需要的是旋转矩形的内容。这是我用来获取矩形角的代码。 (x,y = 图像的中心。图像的宽度、高度)。</p>

<pre><code>UIBezierPath* aPath = ;

//1
[aPath moveToPoint:CGPointMake(
                               x+(width/2)*cosf(A)-(height/2)*sinf(A),
                               y+(height/2)*cosf(A)+(width/2)*sinf(A))];

NSLog(@&#34;%f, %f&#34;, x+(width/2)*cosf(A)-(height/2)*sinf(A),
      y+(height/2)*cosf(A)+(width/2)*sinf(A));


//2
[aPath moveToPoint:CGPointMake(
                               x-(width/2)*cosf(A)-(height/2)*sinf(A),
                               y+(height/2)*cosf(A)-(width/2)*sinf(A))];

NSLog(@&#34;%f, %f&#34;, x-(width/2)*cosf(A)-(height/2)*sinf(A),
      y+(height/2)*cosf(A)-(width/2)*sinf(A));


//3
[aPath moveToPoint:CGPointMake(
                               x-(width/2)*cosf(A)+(height/2)*sinf(A),
                               y-(height/2)*cosf(A)-(width/2)*sinf(A))];

NSLog(@&#34;%f, %f&#34;, x-(width/2)*cosf(A)+(height/2)*sinf(A),
      y-(height/2)*cosf(A)-(width/2)*sinf(A));


//4
[aPath moveToPoint:CGPointMake(
                               x+(width/2)*cosf(A)+(height/2)*sinf(A),
                               y-(height/2)*cosf(A)+(width/2)*sinf(A))];

NSLog(@&#34;%f, %f&#34;, x+(width/2)*cosf(A)+(height/2)*sinf(A),
      y-(height/2)*cosf(A)+(width/2)*sinf(A));



//5
[aPath moveToPoint:CGPointMake(
                               x+(width/2)*cosf(A)-(height/2)*sinf(A),
                               y+(height/2)*cosf(A)+(width/2)*sinf(A))];

NSLog(@&#34;%f, %f&#34;, x+(width/2)*cosf(A)-(height/2)*sinf(A),
      y+(height/2)*cosf(A)+(width/2)*sinf(A));
;
</code></pre>

<p>我在想这样的事情:<a href="http://i50.tinypic.com/30nhpnb.png" rel="noreferrer noopener nofollow">A picture of the problem.</a>
(这里的形状不同。)我希望那个黄色部分是一个新的 UIImage。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>使用您的 <code>UIBezierPath</code> 作为剪切路径。有 <code>addClip</code> 方法。</p>

<p>见 <a href="https://developer.apple.com/library/ios/#documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_paths/dq_paths.html" rel="noreferrer noopener nofollow">the Quartz2D Programming Guide</a> , 特别是 <a href="https://developer.apple.com/library/ios/documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_paths/dq_paths.html#//apple_ref/doc/uid/TP30001066-CH211-TPXREF126" rel="noreferrer noopener nofollow">this part</a>了解详情。</p>

<p>这个想法是创建一个新的位图上下文,使用您的路径应用剪辑,然后在此位图上下文上绘制图像(因此将被剪辑),最后从中生成一个 UIImage。</p>

<p><em>此外,与其自己做一些数学运算来旋转您要用于剪辑的 <code>CGRect</code>,不如创建一个没有任何旋转的 <code>CGRect</code>,并且使用 <code>CGAffineTransform</code> 旋转它。这将避免使用 <code>cos</code>/<code>sin</code> 函数自己进行计算的需要,并使您的代码更易于阅读。</em></p>

<hr/>

<p>[编辑] 这是一个完整的例子:</p>

<ul>
<li>生成贝塞尔路径并对其应用 CGAffineTransform 旋转</li>
<li>使用此贝塞尔路径剪辑图像</li>
</ul>

<p>我确实检查过它,它就像一个魅力。</p>

<pre><code>// Generate a UIBezierPath of a rounded rect rotated by angle radians
-(UIBezierPath*)computePathWithRect:(CGRect)rect
                     cornerRadius:(CGFloat)cornerRadius
                              angle:(CGFloat)radians;
{
// Compute basic path
UIBezierPath* path = ;

// Apply rotation.
// Don&#39;t forget that rotations are around origin 0,0 so if you want to rotate around the rect&#39;s center,
// you need to apply a translation so that the rect&#39;s center is at 0,0, then rotate, and translate it back at its original position.
CGAffineTransform restoreCenterPosition = CGAffineTransformMakeTranslation(CGRectGetMidX(rect), CGRectGetMidY(rect));
CGAffineTransform rotateFromOrigin = CGAffineTransformRotate(restoreCenterPosition,radians);
CGAffineTransform rotateFromRectCenter = CGAffineTransformTranslate(rotateFromOrigin, -CGRectGetMidX(rect), -CGRectGetMidY(rect));
;

return path;
}

// Generate a new image from srcImage but clipped with the given path
-(UIImage*)clipImage:(UIImage*)srcImage withPath:(UIBezierPath*)path
{
UIGraphicsBeginImageContextWithOptions(srcImage.size, NO, 0.0);

// Add clipping path
// Actually UIBezierPath has a method for that, that is equivalent to CGContextClip(currentContext, bezierPath.CGPath), so better use it
;

// Flip coordinates before drawing image as UIKit and CoreGraphics have inverted coordinate system
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, srcImage.size.height);
CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1, -1);
// Draw image, that will thus be clipped, on the bitmap context
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, srcImage.size.width, srcImage.size.height), srcImage.CGImage);

// Generate final (clipped) image
UIImage* clippedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return clippedImage;
}


// Usage example
- (void)doClipping
{
UIImage* originalImage = ...
UIBezierPath* path = [self computePathWithRect:CGRectMake(100,100,184,94)
                                    cornerRadius:10.f
                                           angle:30*M_PI/180.f];
UIImage* clippedImage = ;
self.resultImageView.image = clippedImage;
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 用路径提取 UIImage,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/12265834/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/12265834/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 用路径提取 UIImage