Ios UIButton图像视图仅显示颜色填充

Ios UIButton图像视图仅显示颜色填充,ios,uiimageview,uibutton,Ios,Uiimageview,Uibutton,我正在将UIButton的ImageView.Image属性设置为从ImagePickerController捕获的图像。在我的应用程序中,图像被捕获并用边框圆整。图像已保存,我正在将图像加载到按钮中。问题是,按钮只显示一个蓝色的圆圈,而不是里面的图像。。。这似乎只发生在按钮上 以下是我的代码,用于环绕图像并设置边框: -(UIImage *)makeRoundedImage:(UIImage *) image radius: (float) radius;

我正在将UIButton的ImageView.Image属性设置为从ImagePickerController捕获的图像。在我的应用程序中,图像被捕获并用边框圆整。图像已保存,我正在将图像加载到按钮中。问题是,按钮只显示一个蓝色的圆圈,而不是里面的图像。。。这似乎只发生在按钮上

以下是我的代码,用于环绕图像并设置边框:

-(UIImage *)makeRoundedImage:(UIImage *) image
                  radius: (float) radius;
{
CALayer *imageLayer = [CALayer layer];
imageLayer.frame = CGRectMake(0, 0, image.size.width, image.size.height);
imageLayer.contents = (id) image.CGImage;
imageLayer.backgroundColor = [[UIColor clearColor] CGColor];

imageLayer.masksToBounds = YES;
imageLayer.cornerRadius = radius;
imageLayer.borderWidth = 40;
UIColor *ios7BlueColor = [UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0];
imageLayer.borderColor = ios7BlueColor.CGColor;

UIGraphicsBeginImageContext(image.size);
[imageLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return roundedImage;
}
下面是我设置按钮图像的方式:(player.playerImage是一个cordite字符串,带有保存图像的路径)


这是对UIButton的ImageView实现的限制还是我遗漏了什么?

您实际上并没有将原始图像写入到上下文中

请尝试以下操作:

UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
[imageLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

这会将图像渲染到上下文中,然后是图层。

请阅读UIButton类参考中链接的名为“按钮”的文档。这将解释为什么你会得到蓝色的图像,以及如何处理它。谢谢。这让我找到了正确的地方。我必须将UIRenderingMode设置为始终原始…嗯。我没有看到。当我从另一个视图控制器将图像读入UIImageView时,它显示正确。图像似乎已正确保存,但UIButton.ImageView未正确渲染它。。。
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
[imageLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();