我正在将一个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是一个带有保存图像路径的礼貌字符串)
-(void)ViewController:(UIViewController *)sender didUpdatePlayerImage:(NSString *)playerImagePath
{
player.playerImage = playerImagePath;
[_editImageButton setImage:[UIImage imageWithContentsOfFile:player.playerImage] forState:UIControlStateNormal];
}
这是对UIButton的ImageView实现的限制还是我遗漏了什么?
答案 0 :(得分:0)
您实际上并没有将原始图像写入上下文。
尝试以下方法:
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
[imageLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
这应该将图像渲染到上下文中,然后是图层。