Skip to content

Instantly share code, notes, and snippets.

@qyzhaojinxi
Last active December 10, 2015 18:58
Show Gist options
  • Save qyzhaojinxi/fccee013c7b3b636bb98 to your computer and use it in GitHub Desktop.
Save qyzhaojinxi/fccee013c7b3b636bb98 to your computer and use it in GitHub Desktop.
图片水印 #截屏 #图片加文字 #图片加水印
截屏
UIGraphicsBeginImageContextWithOptions(pageView.page.bounds.size, YES, zoomScale);
[pageView.page.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *uiImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
图片加文字
-(UIImage *)addText:(UIImage *)img text:(NSString *)text1
{
//上下文的大小
int w = img.size.width;
int h = img.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();//创建颜色
//创建上下文
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 44 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);//将img绘至context上下文中
CGContextSetRGBFillColor(context, 0.0, 1.0, 1.0, 1);//设置颜色
char* text = (charchar *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];
CGContextSelectFont(context, "Georgia", 30, kCGEncodingMacRoman);//设置字体的大小
CGContextSetTextDrawingMode(context, kCGTextFill);//设置字体绘制方式
CGContextSetRGBFillColor(context, 255, 0, 0, 1);//设置字体绘制的颜色
CGContextShowTextAtPoint(context, w/2-strlen(text)*5, h/2, text, strlen(text));//设置字体绘制的位置
//Create image ref from the context
CGImageRef imageMasked = CGBitmapContextCreateImage(context);//创建CGImage
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return [UIImage imageWithCGImage:imageMasked];//获得添加水印后的图片
}
水印
-(UIImage *)addImageLogo:(UIImage *)img text:(UIImage *)logo
{
//get image width and height
int w = img.size.width;
int h = img.size.height;
int logoWidth = logo.size.width;
int logoHeight = logo.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
//create a graphic context with CGBitmapContextCreate
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 44 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
CGContextDrawImage(context, CGRectMake(w-logoWidth, 0, logoWidth, logoHeight), [logo CGImage]);
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return [UIImage imageWithCGImage:imageMasked];
// CGContextDrawImage(contextRef, CGRectMake(100, 50, 200, 80), [smallImg CGImage]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment