將View轉(zhuǎn)化為圖片

在view上面添加一張圖片,然后將View轉(zhuǎn)化為一張圖片

+ (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;

// 下面方法,第一個參數(shù)表示區(qū)域大小。第二個參數(shù)表示是否是非透明的。如果需要顯示半透明效果,需要傳NO,否則傳YES。第三個參數(shù)就是屏幕密度了

UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);

[bgView.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return image;

}


//16進制顏色轉(zhuǎn)成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];

}

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容