码迷,mamicode.com
首页 > 移动开发 > 详细

【iOS】iPhone截图并添加水印

时间:2014-12-27 20:25:33      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

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

 

【iOS】iPhone截图并添加水印

标签:

原文地址:http://www.cnblogs.com/onlywish/p/4189020.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!