如何将 UIImageView.image 设置为 UIBarButtonItem

发布于 2024-11-29 22:31:36 字数 953 浏览 1 评论 0原文

我想将 UIImageView.image 中的图像设置到我的栏按钮。当我设置图像并给出其名称(例如@“cameraImageButton.png”)时,栏按钮工作正常,但是当我执行 myBarButton.image = UIImageView.image (并且此 UIImage 存在,包含相同的@“cameraImageButton.png”)时该按钮不显示。

这是不起作用的代码:

UIBarButtonItem *cancelBarButton = [[ UIBarButtonItem alloc ] initWithTitle: @"Cancel" style: UIBarButtonItemStyleBordered target: self action: @selector(done)];
cancelBarButton.image = cancelImageView.image;

这是有效的代码(但我想从 cancelImageView 设置图像):

UIBarButtonItem *saveBarButton = [[ UIBarButtonItem alloc ] initWithTitle: @"Save" style: UIBarButtonItemStyleBordered target: self action: @selector(takePhoto)];
saveBarButton.image = [UIImage imageNamed:[NSString stringWithFormat:@"cameraImageButton.png"]];

//init for flexItem and cancelBarButton

NSArray *items = [NSArray arrayWithObjects: cancelBarButton, flexItem, saveBarButton, nil];
[cameraToolbar setItems:items animated:NO];

I want to set image from UIImageView.image to my bar button. The bar button works fine when i set the image giving it's name (eg. @"cameraImageButton.png"), however when i do myBarButton.image = UIImageView.image (and this UIImage exists, consist same @"cameraImageButton.png") the button doesn't show up.

Here is the code that doesn't work:

UIBarButtonItem *cancelBarButton = [[ UIBarButtonItem alloc ] initWithTitle: @"Cancel" style: UIBarButtonItemStyleBordered target: self action: @selector(done)];
cancelBarButton.image = cancelImageView.image;

And here is the one that works (but i want to set image from cancelImageView):

UIBarButtonItem *saveBarButton = [[ UIBarButtonItem alloc ] initWithTitle: @"Save" style: UIBarButtonItemStyleBordered target: self action: @selector(takePhoto)];
saveBarButton.image = [UIImage imageNamed:[NSString stringWithFormat:@"cameraImageButton.png"]];

//init for flexItem and cancelBarButton

NSArray *items = [NSArray arrayWithObjects: cancelBarButton, flexItem, saveBarButton, nil];
[cameraToolbar setItems:items animated:NO];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

_失温 2024-12-06 22:31:36

UIImageView.image 是一个属性而不是一个对象。它是 UIImageView 类以及 UIBarButtonItem 类中。只能传递 object 或 nil 来设置属性。

UIImageView.image is a property not an object. It is @property(nonatomic, retain) UIImage *image in UIImageView class and also in UIBarButtonItem Class. Only object or nil can be passed to set the property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文