使用stretchableImageWithLeftCapWidth调整图像大小 [英] Resizing an image with stretchableImageWithLeftCapWidth

查看:120
本文介绍了使用stretchableImageWithLeftCapWidth调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用stretchableImageWithLeftCapWidth调整图片大小:它在模拟器上工作,但在设备上,显示垂直的绿色条。



使用imageNamed,imageWithContentOfFile和imageWithData加载图片,但不更改。

  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

查看结果:绿色条不应该出现在模拟器上。 / p>

$ b



似乎这可能是一个与UIGraphicsGetImageFromCurrentImageContext有关的错误。如果我画一个有透明度的图像,我会得到红色/绿色的假象。这些只出现在设备上(不在sim)。此外,如果我从图像中移除透明度,则工件会消失。



更新:有些更奇怪。我之前使用的是PNG,所以我试着使用透明的gif。使用GIF,在模拟器上显示工件问题。



胜利!找到解决方案:



在您的项目的构建设置中关闭压缩PNG文件。停用此选项后,PNG透明度将无任何工件。


I'm trying to resize an image using stretchableImageWithLeftCapWidth: it works on the simulator, but on the device, vertical green bars appears.

I've tried to use imageNamed, imageWithContentOfFile and imageWithData lo load the image, it doesnt change.

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;

See the result: the green bars shouldn't be there, they dont appear on the simulator.

解决方案

Did you figure this out?

Seems like it might be a bug with UIGraphicsGetImageFromCurrentImageContext. If I draw an image that has transparency I get red/green artifacts. These only appear on the device (not in the sim). Also, if I remove transparency from the image, the artifacts go away.

Update: Some more weirdness. I was using PNGs before, so I tried using a transparent gif instead. Using a GIF, shows the artifact problem on the sim.

Victory! Found a solution:

Turn off 'Compress PNG Files' in the build settings for your project. Disabling this makes the PNG transparency work without any artifacts.

这篇关于使用stretchableImageWithLeftCapWidth调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆