gpt4 book ai didi

iphone - 在 monotuch 中通过 CGBitmapContext 组合两个图像时如何避免质量损失

转载 作者:行者123 更新时间:2023-11-28 22:42:56 24 4
gpt4 key购买 nike

我有两张图片。第一个 (avatar) 是 640x640,第二个 (like) 是 21x20。当我试图先画第二张时,我明白了,但质量很差。这是代码:

var bounds = targeImageView.Bounds;
CGImageAlphaInfo alphaInfo = CGImageAlphaInfo.PremultipliedLast;
CGColorSpace colorSpaceInfo = CGColorSpace.CreateDeviceRGB ();
CGBitmapContext resultBitmap = new CGBitmapContext (IntPtr.Zero, (int)bounds.Width, (int)bounds.Height, 8, 4 * (int)bounds.Width, colorSpaceInfo, alphaInfo);
resultBitmap.DrawImage (bounds, avatar.CGImage);
var likeY = 0;
var likeX = 0;
var likeHeight = (float)Math.Floor (bounds.Height / 2);
var likeWidth = likeHeight;
resultBitmap.DrawImage (new RectangleF (likeX, likeY, likeWidth, likeHeight), like.CGImage);
targetImageView.Image = UIImage.FromImage (resultBitmap.ToImage ());

这里是avatar图片在同一个image view但是没有上层代码,只是imageView.Image = avatar;

avatar

这里是相同的,但在绘制之后。

enter image description here

最佳答案

你的视力比我好 - 至少在这么小的尺寸下。如果它是视网膜问题(我认为是),那么以下代码应该可以帮助您,因为它创建了一个上下文,可以将自身调整为(或不)视网膜显示。

UIImage avatar = ...;
UIImage like = ...;
UIGraphics.BeginImageContextWithOptions (avatar.Size, false, 0);
using (CGContext ctx = UIGraphics.GetCurrentContext ()) {
var rect = new RectangleF (Point.Empty, avatar.Size);
avatar.Draw (rect);
// coordinates are likely reversed (UIKit wrt CoreGraphics) you might have to fix them (untried)
rect = new RectangleF (0, avatar.Height / 2, avatar.Width / 2, avatar.Height / 2);
like.Draw (rect);

targetImageView.Image = UIGraphics.GetImageFromCurrentImageContext ();
UIGraphics.EndImageContext ();
// dispose of avatar and like UIImage if not used anymore
}

关于iphone - 在 monotuch 中通过 CGBitmapContext 组合两个图像时如何避免质量损失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13977054/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com