liwei's blog

截取整个scrollView和16进制颜色转换UIColor

截取整个scrollView,包含屏幕上未显示的部分,直接返回UIImage

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
-(UIImage *)captureScrollView:(UIScrollView *)scrollView
{
UIImage* image = nil;
UIGraphicsBeginImageContext(scrollView.contentSize);
{
CGPoint savedContentOffset = scrollView.contentOffset;
CGRect savedFrame = scrollView.frame;
scrollView.contentOffset = CGPointZero;
scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);
[scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
scrollView.contentOffset = savedContentOffset;
scrollView.frame = savedFrame;
}
UIGraphicsEndImageContext();
if (image != nil) {
return image;
}
return nil;
}

16进制颜色表示以0x开头后面接数字和字母的,比如0x000000表示黑色的,直接传递0x开头的字符串,返回UIColor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- (UIColor *)getColor:(NSString *)hexColor
{
unsigned int red,green,blue;
NSRange range;
range.length = 2;

range.location = 0;
[[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&red];

range.location = 2;
[[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&green];

range.location = 4;
[[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&blue];

return [UIColor colorWithRed:(float)(red/255.0f) green:(float)(green / 255.0f) blue:(float)(blue / 255.0f) alpha:1.0f];

}