[英]Resize UIImage with aspect ratio?
我正在使用以下代碼來調整iPhone上圖像的大小:
CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0);
UIGraphicsBeginImageContext(screenRect.size);
[value drawInRect:screenRect blendMode:kCGBlendModePlusDarker alpha:1];
UIImage *tmpValue = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
只要圖像的縱橫比與新調整大小的圖像的縱橫比匹配,該方法就很好用。 我想對此進行修改,以使其保持正確的寬高比,並在未顯示圖像的任何地方放置黑色背景。 因此,我仍然會得到一張320x480的圖像,但頂部和底部或側面為黑色,具體取決於原始圖像的大小。
有沒有一種類似於我正在做的簡單方法? 謝謝!
設置屏幕矩形后,請執行以下類似操作,以決定在哪個矩形上繪制圖像:
float hfactor = value.bounds.size.width / screenRect.size.width;
float vfactor = value.bounds.size.height / screenRect.size.height;
float factor = fmax(hfactor, vfactor);
// Divide the size by the greater of the vertical or horizontal shrinkage factor
float newWidth = value.bounds.size.width / factor;
float newHeight = value.bounds.size.height / factor;
// Then figure out if you need to offset it to center vertically or horizontally
float leftOffset = (screenRect.size.width - newWidth) / 2;
float topOffset = (screenRect.size.height - newHeight) / 2;
CGRect newRect = CGRectMake(leftOffset, topOffset, newWidth, newHeight);
如果您不想放大小於screenRect的圖像,請確保factor
大於或等於1(例如factor = fmax(factor, 1)
)。
要獲得黑色背景,您可能只想將上下文顏色設置為黑色,並在繪制圖像之前調用fillRect。
我知道這很老了,但是感謝那篇文章-它使我從嘗試使用比例尺重定向到繪制圖像。 萬一對任何人都有利,我做了一個擴展類,我將在這里進行介紹。 它允許您調整圖像的大小,如下所示:
UIImage imgNew = img.Fit(40.0f, 40.0f);
我不需要合適的選項,但可以輕松擴展它以支持Fill。
using CoreGraphics;
using System;
using UIKit;
namespace SomeApp.iOS.Extensions
{
public static class UIImageExtensions
{
public static CGSize Fit(this CGSize sizeImage,
CGSize sizeTarget)
{
CGSize ret;
float fw;
float fh;
float f;
fw = (float) (sizeTarget.Width / sizeImage.Width);
fh = (float) (sizeTarget.Height / sizeImage.Height);
f = Math.Min(fw, fh);
ret = new CGSize
{
Width = sizeImage.Width * f,
Height = sizeImage.Height * f
};
return ret;
}
public static UIImage Fit(this UIImage image,
float width,
float height,
bool opaque = false,
float scale = 1.0f)
{
UIImage ret;
ret = image.Fit(new CGSize(width, height),
opaque,
scale);
return ret;
}
public static UIImage Fit(this UIImage image,
CGSize sizeTarget,
bool opaque = false,
float scale = 1.0f)
{
CGSize sizeNewImage;
CGSize size;
UIImage ret;
size = image.Size;
sizeNewImage = size.Fit(sizeTarget);
UIGraphics.BeginImageContextWithOptions(sizeNewImage,
opaque,
1.0f);
using (CGContext context = UIGraphics.GetCurrentContext())
{
context.ScaleCTM(1, -1);
context.TranslateCTM(0, -sizeNewImage.Height);
context.DrawImage(new CGRect(CGPoint.Empty, sizeNewImage),
image.CGImage);
ret = UIGraphics.GetImageFromCurrentImageContext();
}
UIGraphics.EndImageContext();
return ret;
}
}
}
按照上面的文章,它為圖像啟動了一個新的上下文,然后為該圖像找出縱橫比,然后繪制到圖像中。 如果您還沒有完成任何Swift xcode開發時間,那么UIGraphics對於我使用的大多數系統都會有些落后,但還不錯。 一個問題是,默認情況下,位圖從下至上繪制。 為了解決這個問題,
context.ScaleCTM(1, -1);
context.TranslateCTM(0, -sizeNewImage.Height);
將圖形的方向更改為更常見的左上角到右下角...,但是隨后還需要移動原點,因此也需要移動TranslateCTM。
希望它可以節省一些時間。
干杯
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.