[英]How to avoid quality loss when combining two images via CGBitmapContext in monotouch
I have two images. 我有两张照片。 First (
avatar
) is 640x640, second ( like
) is 21x20. 第一个(
avatar
)是640x640,第二个( like
)是21x20。 When I'm trying to draw second on first, I get it, but with very low quality. 当我第一次尝试绘制第二个时,我得到它,但质量非常低。 Here is the code:
这是代码:
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 ());
Here is the avatar
picture on the same image view but without upper code, just imageView.Image = avatar;
这是同一图像视图上的
avatar
图片,但没有上层代码,只有imageView.Image = avatar;
Here is the same but after drawing. 这是相同的,但绘制后。
You got better eyesight than me - at least at that small size. 你的视力比我好 - 至少在那么小的范围内。 If it's a retina issue (I think so) then the following code should help you as it creates a context that will adjust itself to a retina (or not) display.
如果它是一个视网膜问题(我想是这样),那么下面的代码应该可以帮助你,因为它创建了一个上下文,可以将自己调整为视网膜(或不显示)。
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
}
Use PNGs they are lossless, and better quality than JPG or Bitmap. 使用PNG,它们是无损的,质量比JPG或Bitmap更好。
NSData *imageData = UIImagePNGRepresentation(image);
NSData * imageData = UIImagePNGRepresentation(image);
UIImage *image=[UIImage imageWithData:data];
UIImage * image = [UIImage imageWithData:data];
Allocate UIImageView with frame for placement using CGRectMake (). 使用CGRectMake()为框架分配UIImageView以进行放置。 You will be able to float the image anywhere you want.
您可以将图像浮动到任何您想要的位置。
Set the ImageView's Image with [ImageView setImage:...] 使用[ImageView setImage:...]设置ImageView的图像
The Image will be rendered much cleaner and you will have full control over quality and placement. 图像将变得更清晰,您将完全控制质量和位置。
I have used a 256 image compressed down to a 15px x 15px and it still held a very good shape. 我使用压缩到15px x 15px的256个图像,它仍然保持非常好的形状。 In contrast I have also stretched images the other way around.
相比之下,我也反过来拉伸图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.