iOS如何将图像的一部分绘制到特定的矩形?
<p><p>我想裁剪一张 200*200 的图片,裁剪的部分是 (x=10,y=10,w=50,h=50)。并将这部分绘制到新的 500*500 图像上,新的矩形是 (x=30,y=30,w=50, h=50),怎么做? </p>
<p>我可以通过以下方法获取图像的一部分</p>
<pre><code>- (UIImage*) getSubImageWithRect: (CGRect) rect {
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
// translated rectangle for drawing sub image
CGRect drawRect = CGRectMake(-rect.origin.x, -rect.origin.y, self.size.width, self.size.height);
// clip to the bounds of the image context
// not strictly necessary as it will get clipped anyway?
CGContextClipToRect(context, CGRectMake(0, 0, rect.size.width, rect.size.height));
// draw image
;
// grab image
UIImage* subImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return subImage;
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>让我们尝试使用以下代码块</p>
<pre><code>- (UIImage*) getSubImageFromImage:(UIImage *)image
{
// translated rectangle for drawing sub image
CGRect drawRect = CGRectMake(10, 10, 50, 50);
// Create Image Ref on Image
CGImageRef imageRef = CGImageCreateWithImageInRect(, rect);
// Get Cropped Image
UIImage *img = ;
CGImageRelease(imageRef);
return img;
}
</code></pre></p>
<p style="font-size: 20px;">关于iOS如何将图像的一部分绘制到特定的矩形?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/33206077/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/33206077/
</a>
</p>
页:
[1]