使用stretchableImageWithLeftCapWidth调整图像大小

Ste*_*lot 7 iphone cocoa-touch

我正在尝试使用stretchableImageWithLeftCapWidth调整图像大小:它在模拟器上工作,但在设备上,会出现垂直绿条.

我试图使用imageNamed,imageWithContentOfFile和imageWithData lo加载图像,它没有改变.

UIImage *bottomImage = [[UIImage imageWithData:
     [NSData dataWithContentsOfFile:
    [NSString stringWithFormat:@"%@/bottom_part.png", 
     [[NSBundle mainBundle] resourcePath]]]] 
       stretchableImageWithLeftCapWidth:27 topCapHeight:9];

UIImageView *bottomView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 200+73, 100, 73)];
[self.view addSubview:bottomView];
UIGraphicsBeginImageContext(CGSizeMake(100, 73));
[bottomImage drawInRect:CGRectMake(0, 0, 100, 73)];
UIImage *bottomResizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
bottomView.image = bottomResizedImage;
Run Code Online (Sandbox Code Playgroud)

看到结果:绿色条不应该在那里,它们不会出现在模拟器上.

替代文字http://www.quicksnapper.com/files/5161/96232162149F1C14751048_m.png

Nat*_*ner 12

你搞清楚了吗?

看起来它可能是UIGraphicsGetImageFromCurrentImageContext的错误.如果我绘制一个具有透明度的图像,我会得到红色/绿色的文物.这些只出现在设备上(不在SIM卡中).此外,如果我从图像中删除透明度,工件就会消失.

更新:更奇怪一些.我之前使用的是PNG,所以我尝试使用透明的gif代替.使用GIF,显示SIM卡上的工件问题.

胜利!找到了解决方案:

在项目的构建设置中关闭"压缩PNG文件".禁用此选项可使PNG透明度无任何工件.


Chr*_*ett 5

好像你写了很多不必要的代码,但也许这只是因为它是断章取义,还有更多的是我失踪.

要获得图像:

[[UIImage imageName: @"bottom_part.png"] stretchableImageWithLeftCapWidth: 27 topCapHeight: 9];
Run Code Online (Sandbox Code Playgroud)

您显示此图像的代码的哪一部分?即你用什么方法调用上面的图像?

为什么不使用UIImageView并将图像放在那里?那你就不需要做任何图像上下文绘图等.