我正在将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字符串)
-(void)ViewController:(UIViewController *)sender didUpdatePlayerImage:(NSString *)playerImagePath
{
player.playerImage = playerImagePath;
[_editImageButton setImage:[UIImage imageWithContentsOfFile:player.playerImage] forState:UIControlStateNormal];
}这是对UIButton的ImageView实现的限制,还是我遗漏了什么?
发布于 2014-06-24 07:03:55
您实际上并没有将原始图像写入到上下文中。
尝试以下操作:
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
[imageLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();这应该将图像渲染到上下文中,然后是您的层。
https://stackoverflow.com/questions/24375887
复制相似问题