3

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

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

在我的应用程序中渲染图像并导出它时,我看到的唯一区别是我使用 UIImageViews 来保存可拉伸图像,而导出我只是使用 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];
4

1 回答 1

3

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

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

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

于 2012-06-13T11:26:55.153 回答