// 添加一个加号按钮 UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom]; [addButton setImage:[UIImage imageNamed:@"tag_add_icon"] forState:UIControlStateNormal]; addButton.x = TbTopicCellMargin; addButton.size = addButton.currentImage.size; [addButton addTarget:self action:@selector(add) forControlEvents:UIControlEventTouchUpInside]; [self.topView addSubview:addButton]; self.addButton = addButton;
1>咱们知道imageNamed是有缓存的,从新再次拿到同一个图片名称,内存地址打印是相同的,没必要担忧又加载了一次图片缓存
addButton.size = [UIImage imageNamed:@"tag_add_icon"].size;
2>根据状态获取图片大小ide
addButton.size = [addButton imageForState:UIControlStateNormal].size;
3>根据当前图片获取图片大小code
addButton.size = addButton.currentImage.size;