将可拉伸的 UIImage 与“正常”图像合并一

发布于 2024-10-16 22:14:43 字数 1295 浏览 1 评论 0原文

我想组合两个 UIImage,一个是可拉伸的,一个是“正常”的。问题是,如果我使用 UIGraphicsImageContext 合并图像,第二个图像也会被拉伸(它应该在第一个图像的顶部,但被拉伸)。有人知道如何避免这种情况吗?

多谢!

来自我的 ViewController 的调用:

UIImage *stretchImage = [[UIImage imageNamed:@"stretchableLeft.png"]   stretchableImageWithLeftCapWidth:0.0 topCapHeight:16.0];
    stretchImage = [self imageWithImage:stretchImage scaledToSize:CGSizeMake(64.0, 64.0)];
    stretchImage = [self mergeImageWithImage:stretchImage secondImage:[UIImage imageNamed:@"topImage.png"]]; // only 40x40 Px

这两种方法是:

- (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize  
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage;


- (UIImage*)mergeImageWithImage:(UIImage *)image secondImage:(UIImage *)image2 
{
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
[image2 drawInRect:CGRectMake(10,10,image.size.width,image.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage;
}

I'd like to combine two UIImages, one stretchable and one "normal" one. The problem is that if I merge the Images using the UIGraphicsImageContext, the scond image is also stretched (it is on top of the first one as it should be, but stretched). Does anybody know how to avoid this?

Thanks a lot!

calls from my ViewController:

UIImage *stretchImage = [[UIImage imageNamed:@"stretchableLeft.png"]   stretchableImageWithLeftCapWidth:0.0 topCapHeight:16.0];
    stretchImage = [self imageWithImage:stretchImage scaledToSize:CGSizeMake(64.0, 64.0)];
    stretchImage = [self mergeImageWithImage:stretchImage secondImage:[UIImage imageNamed:@"topImage.png"]]; // only 40x40 Px

the two methods are:

- (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize  
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage;


- (UIImage*)mergeImageWithImage:(UIImage *)image secondImage:(UIImage *)image2 
{
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
[image2 drawInRect:CGRectMake(10,10,image.size.width,image.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉生梦死 2024-10-23 22:14:43

我认为问题在于您要求两个图像都绘制在完整的矩形中。这会导致你的第二张图像拉伸。

合并图像时尝试使用 image2.size。绘制矩形时,您必须使用 x/y 坐标调整位置。

- (UIImage*)mergeImageWithImage:(UIImage *)image secondImage:(UIImage *)image2 
{
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
[image2 drawInRect:CGRectMake(10,10,image2.size.width,image2.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage;
}

I think the issue is that you're asking both images to draw in the full rectangle. That is causing your second image to stretch.

Try using the image2.size for image2 when merging the images. You'll have to adjust the placement using the x/y coordinate when drawing the rectangle.

- (UIImage*)mergeImageWithImage:(UIImage *)image secondImage:(UIImage *)image2 
{
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
[image2 drawInRect:CGRectMake(10,10,image2.size.width,image2.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文