ios - 如何将任意大小的图像裁剪到特定尺寸?
<p><p>我的要求是当我处理任何尺寸的图像时,该图像应自动裁剪为 320x240 的尺寸而无需任何拉伸(stretch)。如何为此功能编写裁剪方法?我的意思是我有各种尺寸的图像列表,如果我从这个列表中选择一个图像,它将自动转换为 320x 240 的尺寸。我该如何实现它。请给我建议。</p>
<p>现在我使用以下编码:
<a href="http://www.abdus.me/ios-programming-tips/resize-image-in-ios/" rel="noreferrer noopener nofollow">http://www.abdus.me/ios-programming-tips/resize-image-in-ios/</a> </p>
<p>但是生成的图像质量不好,它会变得模糊。你能告诉我如何解决这个问题。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p><strong><em>对于裁剪 - 图像部分:</em></strong></p>
<pre><code>- (UIImage*) getSubImageFrom: (UIImage*) img WithRect: (CGRect) rect {
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
// translated rectangle for drawing sub image
CGRect drawRect = CGRectMake(-rect.origin.x, -rect.origin.y, img.size.width, img.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><strong><em>调整大小</em></strong> - </p>
<p>把UIImageView的frame放到320x240然后<code>imageView.contentMode = UIViewContentModeScaleAspectFit;</code></p></p>
<p style="font-size: 20px;">关于ios - 如何将任意大小的图像裁剪到特定尺寸?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/24239815/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/24239815/
</a>
</p>
页:
[1]