UIImage 显示一个空方块而不是图像

UIImage shows an empty square instead of image

我尝试在导航面板上显示 png 图片。我这样做:

UIImage *img = [UIImage imageNamed:@"1.png"];
UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithImage:img style:UIBarButtonItemStylePlain target:self action:@selector(myAction)];
self.navigationItem.rightBarButtonItems = @[myButton];

img 不是 nil,但是 myButton 显示为白色方块。这个方块的大小和我的image一样大,但是没有显示图像,只是一个白色的方块。我有一个 @2x 版本的视网膜图像,我选择了我的项目作为这两个图像的目标。

有什么问题吗?

检查 Human Interface Guidelines 工具栏和导航栏图标(可选):

无论图标的视觉风格如何,创建以下尺寸的工具栏或导航栏图标:

大约 22 x 22 像素(标准分辨率)

大约 44 x 44 像素 (2x)

大约 66 x 66 像素 (3x)

代码:

UIImage *image = [UIImage imageNamed:@"image.png"];
UIImage *newImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithImage:newImage style:UIBarButtonItemStylePlain target:self action:@selector(myAction)];
self.navigationItem.rightBarButtonItems = @[myButton];
UIImage *img = [UIImage imageNamed:@"1"];**//1.png previously**

UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithImage:img style:UIBarButtonItemStylePlain target:self action:@selector(myAction)];
self.navigationItem.rightBarButtonItems = @[myButton];

只需删除 .png 只需提供图片名称即可。