将可拉伸的 UIImage 与“正常”图像合并一
我想组合两个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题在于您要求两个图像都绘制在完整的矩形中。这会导致你的第二张图像拉伸。
合并图像时尝试使用 image2.size。绘制矩形时,您必须使用 x/y 坐标调整位置。
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.