+(UIImage *)screenForView:(UIView *)view
{
// UIGraphicsBeginImageContext(view.frame.size);
// CGContextRef context = UIGraphicsGetCurrentContext();
// [view.layer renderInContext:context];
// UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
// UIGraphicsEndImageContext();
//
// return theImage;
size_t width = view.frame.size.width;
size_t height = view.frame.size.height;
size_t bitsPerComponent = 8; //这个我不太懂,大概意思是表示一个颜色职的位数
size_t bytesPerRow = width * 4; //每一行的字节数,ARGB 每一个位数是8,也就是一个字节 ,每一个像索占4个字节
CGColorSpaceRef spaceRef = CGColorSpaceCreateDeviceRGB(); //这个命名空间和灰度化有关系
void * data = malloc(bytesPerRow * height); //文档上有说明
CGContextRef context = CGBitmapContextCreate(data, width, height, bitsPerComponent, bytesPerRow, spaceRef, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
if (context == NULL) {
NSLog(@"创建绘图环境失败");
}
//旋转画面
//先翻转画布 — 仿射距阵很让人纠结,之后我会写一篇关于仿射距阵的文章
CGContextTranslateCTM(context, width, 0);
CGContextScaleCTM(context, -1, 1);
CGContextTranslateCTM(context, width, height);
CGContextRotateCTM(context, radians(-180.0));
//到目前为卡,坐标旋转的和ios的坐标一至了
CGContextSaveGState(context);//保存当前的状态
CGContextClearRect(context, CGRectMake(0, 0, width, height));
// CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]);
// CGContextFillRect(context, CGRectMake(0, 0, width, height));
//CGContextClipToRect(context, CGRectMake(0, 0, width, height));
[[view layer] renderInContext:context]; //配送到这个context
//添加水印
//变化坐标,frame在这里没有用
CGContextTranslateCTM(context, 220, height-50-10);
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, height-50, 100, 50)];
label.text = @"惟愿";
[label setShadowColor:[UIColor grayColor]];
[label setTextColor:[UIColor whiteColor]];
[label setBackgroundColor:[UIColor clearColor]];
[label setFont:[UIFont boldSystemFontOfSize:20]];
[label setTextAlignment:NSTextAlignmentCenter];
[[label layer] renderInContext:context];
[label release];
//添加水印2
CGContextTranslateCTM(context, 0, 40);
UILabel * label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, height-50, 100, 10)];
label2.text = @"http:/onlywish.me";
[label2 setShadowColor:[UIColor grayColor]];
[label2 setTextColor:[UIColor whiteColor]];
[label2 setBackgroundColor:[UIColor clearColor]];
[label2 setFont:[UIFont boldSystemFontOfSize:12]];
[label2 setTextAlignment:NSTextAlignmentCenter];
[[label2 layer] renderInContext:context];
[label2 release];
CGImageRef imageRef = CGBitmapContextCreateImage(context);
UIImage * result = [UIImage imageWithCGImage:imageRef];
//释放内存
CGColorSpaceRelease(spaceRef);
CGImageRelease(imageRef);
CGContextRelease(context);
free(data);
return result;
}