如何在 objective c 中将个人资料图片放置在带有圆角的 uibarbuttonitem 上?
How to place profile image on uibarbuttonitem with rounded corners in objective c?
我想使我的 UIbarbuttonItem 具有圆角(右侧)。 I referred the link to implement 还是我不是 getting.The 图片来自 URL.My 代码是,
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:userImage]];
if ( data == nil )
return;
dispatch_async(dispatch_get_main_queue(), ^{
// WARNING: is the cell still using the same data by this point??
UIImage *img = [UIImage imageWithData: data];
UIImage *btnImage = [self imageWithImage:img scaledToSize:CGSizeMake(50, 50)];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
// btn.bounds = CGRectMake( 0, 0, btnImage.size.width, btnImage.size.height );
[btn addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchDown];
[btn setImage:btnImage forState:UIControlStateNormal];
UIBarButtonItem *btnItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.rightBarButtonItem = btnItem;
});
});
我需要执行与 image.What 相同的操作,我做错了,请任何人帮助我解决这个问题。
你可以使用这个
使按钮变圆
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0,0,100,100)//set your frame's size
[btn setImage:[UIImage imageNamed:btnImage] forState:UIControlStateNormal];
btn.layer.masksToBounds = NO;
btn.layer.cornerRadius = btn.bounds.size.width/2;;
UIBarButtonItem * btnItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
[self.navigationItem setRightBarButtonItems:@[btnItem]];
对我有用:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString* url = @"http://i.stack.imgur.com/EbYBY.jpg";
NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:url]];
UIImage* img = [UIImage imageWithData:data];
UIImageView* v = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
v.image = img;
v.layer.masksToBounds = YES;
v.layer.cornerRadius = 20;
UIBarButtonItem* rightBtn = [[UIBarButtonItem alloc] initWithCustomView:v];
self.navigationItem.rightBarButtonItem = rightBtn;
}
这里是 oxigen 的(优秀)答案转换为 Swift 3 / Swift 4:
override func viewDidLoad()
{
super.viewDidLoad()
let url = URL(string: "http://i.stack.imgur.com/EbYBY.jpg")!
let data = try! Data(contentsOf: url)
let img = UIImage(data: data)
let imageView = UIImageView(frame: CGRect(x: 0.0, y: 0.0, width: 40.0, height: 40.0))
imageView.image = img?.withRenderingMode(.alwaysOriginal)
imageView.layer.cornerRadius = 20.0
imageView.layer.masksToBounds = true
let barButton = UIBarButtonItem(customView: imageView)
navigationItem.setLeftBarButton(barButton, animated: false)
}
(给定的 URL 现在已失效,但是使用适当的 URL 或其他图像,代码应该可以工作。)
我想使我的 UIbarbuttonItem 具有圆角(右侧)。 I referred the link to implement 还是我不是 getting.The 图片来自 URL.My 代码是,
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:userImage]];
if ( data == nil )
return;
dispatch_async(dispatch_get_main_queue(), ^{
// WARNING: is the cell still using the same data by this point??
UIImage *img = [UIImage imageWithData: data];
UIImage *btnImage = [self imageWithImage:img scaledToSize:CGSizeMake(50, 50)];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
// btn.bounds = CGRectMake( 0, 0, btnImage.size.width, btnImage.size.height );
[btn addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchDown];
[btn setImage:btnImage forState:UIControlStateNormal];
UIBarButtonItem *btnItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.rightBarButtonItem = btnItem;
});
});
你可以使用这个
使按钮变圆UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0,0,100,100)//set your frame's size
[btn setImage:[UIImage imageNamed:btnImage] forState:UIControlStateNormal];
btn.layer.masksToBounds = NO;
btn.layer.cornerRadius = btn.bounds.size.width/2;;
UIBarButtonItem * btnItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
[self.navigationItem setRightBarButtonItems:@[btnItem]];
对我有用:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString* url = @"http://i.stack.imgur.com/EbYBY.jpg";
NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:url]];
UIImage* img = [UIImage imageWithData:data];
UIImageView* v = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
v.image = img;
v.layer.masksToBounds = YES;
v.layer.cornerRadius = 20;
UIBarButtonItem* rightBtn = [[UIBarButtonItem alloc] initWithCustomView:v];
self.navigationItem.rightBarButtonItem = rightBtn;
}
这里是 oxigen 的(优秀)答案转换为 Swift 3 / Swift 4:
override func viewDidLoad()
{
super.viewDidLoad()
let url = URL(string: "http://i.stack.imgur.com/EbYBY.jpg")!
let data = try! Data(contentsOf: url)
let img = UIImage(data: data)
let imageView = UIImageView(frame: CGRect(x: 0.0, y: 0.0, width: 40.0, height: 40.0))
imageView.image = img?.withRenderingMode(.alwaysOriginal)
imageView.layer.cornerRadius = 20.0
imageView.layer.masksToBounds = true
let barButton = UIBarButtonItem(customView: imageView)
navigationItem.setLeftBarButton(barButton, animated: false)
}
(给定的 URL 现在已失效,但是使用适当的 URL 或其他图像,代码应该可以工作。)