IOS 如何根据实际图像比例调整图像大小并给出自定义大小?
<p><p>使用 <code>custom size</code> 调整 <code>UIImage</code> 的大小,方法是检查实际图像比例以减小图像大小。我已经尝试过这个 <code>UIImageJPEGRepresentation(Image, 0.8);</code> 但是这个问题是图像质量正在下降。任何帮助将非常感激。谢谢</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>将 <code>UIImage</code> 传递给下面的方法,该方法使用自定义高度/宽度调整图像大小,您还可以提供图像的压缩值以减小图像大小并在调整大小之前和之后检查图像大小。</p>
<p>这里是示例方法-</p>
<pre><code>-(UIImage *)resizeImage:(UIImage *)image
{
NSInteger imageActualSize = UIImageJPEGRepresentation(image,1).length;
NSLog(@"size of IMAGE before resizing: %@ ", );
float actualHeight = image.size.height;
float actualWidth = image.size.width;
float maxHeight = 400.0; // your customheight
float maxWidth = 350; // your customwidth
float imgRatio = actualWidth/actualHeight;
float maxRatio = maxWidth/maxHeight;
float compressionQuality = 0.5;//50 percent compression
if (actualHeight > maxHeight || actualWidth > maxWidth)
{
if(imgRatio < maxRatio)
{
//adjust width according to maxHeight
imgRatio = maxHeight / actualHeight;
actualWidth = imgRatio * actualWidth;
actualHeight = maxHeight;
}
else if(imgRatio > maxRatio)
{
//adjust height according to maxWidth
imgRatio = maxWidth / actualWidth;
actualHeight = imgRatio * actualHeight;
actualWidth = maxWidth;
}
else
{
actualHeight = maxHeight;
actualWidth = maxWidth;
}
}
CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
UIGraphicsBeginImageContext(rect.size);
;
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
NSData *imageData = UIImageJPEGRepresentation(img, compressionQuality);
UIGraphicsEndImageContext();
NSInteger imageReduceSize = imageData.length;
NSLog(@"size of IMAGE after resizing: %@ ",);
return ;
}
</code></pre>
<p>这会对你有所帮助..:)</p></p>
<p style="font-size: 20px;">关于IOS 如何根据实际图像比例调整图像大小并给出自定义大小?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/36998350/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/36998350/
</a>
</p>
页:
[1]