如何将 UIImageView.image 设置为 UIBarButtonItem
我想将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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 inUIBarButtonItem
Class. Only object or nil can be passed to set the property.