ios - 将 UIImage 导出为 stretchableImage 导出带有在 Image 上绘制的水平线和垂直线的图像

标签 ios uiimage

我试图构建一个应用程序,它将背景图像与上面的文本气泡结合在一起。我将文本气泡初始化为可拉伸(stretch)的 UIImage,并定义了 leftCapWidth 和 topCapHeight。在应用程序本身中显示文本气泡时,一切正常,文本气泡正常显示,上面没有绘制任何线条。

但是,当我尝试导出图像时,我使用 UIGraphicsBeginImageContext() 函数将背景图像与顶部的文本气泡组合在一起,我看到文本气泡上绘制了一条垂直线和一条水平线。它们似乎是在由 leftCapWidth 和 topCapHeight 定义的点处绘制的。以前有人遇到过这样的问题吗?

在我的应用程序中渲染图像并将其导出时,我看到的唯一区别是我使用 UIImageViews 来保存可拉伸(stretch)图像,而我只使用 UIImages。

将气泡图像组合到背景图像的代码:

UIGraphicsBeginImageContext(mainImage.size);

[fullSectionImage drawInRect:CGRectMake(0, 0, mainImage.size.width, mainImage.size.height) blendMode:kCGBlendModeNormal alpha:1.0 ];

UIImage *bubbleImage = [UIImage imageNamed:@"bubble1.png"];
stretchableImage = [bubbleImage stretchableImageWithLeftCapWidth:40 topCapHeight:15];
[stretchableImage drawInRect:CGRectMake(originX,originY,width,height) blendMode: kCGBlendModeNormal alpha:1.0 ];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();   
UIImageWriteToSavedPhotosAlbum(newImage, nil,nil,nil);

在应用中显示与背景图片相同的气泡的代码:

UIImageView *bubbleImageView;
UIImage *bubbleImage = [UIImage imageNamed:@"bubble1.png"];
stretchableImage = [bubbleImage stretchableImageWithLeftCapWidth:40 topCapHeight:15];
bubbleImageView = [[UIImageView alloc] initWithImage:stretchableImage];
[bubbleImageView setFrame:CGRectMake(0,0,width,height)];
bubbleImageView.autoresizingMask = YES;
[self addSubview:bubbleImageView];

最佳答案

为了解决这个问题,stretchableImage 应该使用 drawInRect 方法并将所有整数值作为参数。

[stretchableImage drawInRect:CGRectMake(originX,originY,width,height) blendMode: kCGBlendModeNormal alpha:1.0 ];

在这种情况下,originX、originY、width、height 需要是整数值。这为我解决了这个问题。

关于ios - 将 UIImage 导出为 stretchableImage 导出带有在 Image 上绘制的水平线和垂直线的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10966659/

相关文章:

javascript - 在 ios 应用程序中发送包含 javascript 的 html 电子邮件

ios - xCode Cocoapods 无法构建项目 ios(9.3)

ios - 如何为位于 LaunchScreen 上的 UIImageView 添加边框?

ios - 在 Swift 2.0 中从 iOS 应用程序上传图像到服务器

ios - 如何使用 Swift 在现有图像中添加背景颜色?

ios - 调整屏幕截图图像大小导致文本模糊 ios

c++ - 在 IOS 中使用 OpenCV

ios - 在 iOS 设备中单击按钮后,CSS 悬停样式仍然存在

objective-c - 是否有可能在运行时检测到您的 iOS 应用程序正在 iPad mini 上运行?

ios - 在 UIView 上绘制时 UIImage 反转