在view上面添加一张图片,然后将View转化为一张图片
+ (UIImage *)getBgViewFrame:(CGRect)rect withDefaultImage:(UIImage *)placeImage {
UIView *bgView = [[UIView alloc]initWithFrame:rect];
bgView.backgroundColor =[self colorFromHexString:@"#f0f0f0"];
UIImageView *imgView = [[UIImageView alloc]init];
imgView.image = placeImage;
[bgView addSubview:imgView];
imgView.sd_layout
.centerXEqualToView(bgView)
.centerYEqualToView(bgView)
.widthIs(placeImage.size.width)
.heightIs(placeImage.size.height);
CGSize size = rect.size;
// 下面方法,第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕密度了
UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
[bgView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
//16进制颜色转成UIColor类型
- (UIColor *) colorFromHexString:(NSString *)hexString {
NSString *cleanString = [hexString stringByReplacingOccurrencesOfString:@"#" withString:@""];
{
if([cleanString length] == 3) {
cleanString = [NSString stringWithFormat:@"%@%@%@%@%@%@",
[cleanString substringWithRange:NSMakeRange(0, 1)],[cleanString substringWithRange:NSMakeRange(0, 1)],
[cleanString substringWithRange:NSMakeRange(1, 1)],[cleanString substringWithRange:NSMakeRange(1, 1)],
[cleanString substringWithRange:NSMakeRange(2, 1)],[cleanString substringWithRange:NSMakeRange(2, 1)]];
}
}
if([cleanString length] == 6) {
cleanString = [cleanString stringByAppendingString:@"ff"];
}
unsigned int baseValue;
[[NSScanner scannerWithString:cleanString] scanHexInt:&baseValue];
float red = ((baseValue >> 24) & 0xFF)/255.0f;
float green = ((baseValue >> 16) & 0xFF)/255.0f;
float blue = ((baseValue >> 8) & 0xFF)/255.0f;
float alpha = ((baseValue >> 0) & 0xFF)/255.0f;
return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}
网友评论