首页 > 代码库 > iOS UIImage 图片水印,图片裁剪,屏幕截图,背景平铺

iOS UIImage 图片水印,图片裁剪,屏幕截图,背景平铺

图片水印功能

 1 #import "UIImage+ZR.h" 2  3 @implementation UIImage (ZR) 4 + (instancetype)waterImageWithBg:(NSString *)bg logo:(NSString *)logo 5 { 6     UIImage *bgImage = [UIImage imageNamed:bg]; 7      8     // 1.创建一个基于位图的上下文(开启一个基于位图的上下文) 9     UIGraphicsBeginImageContextWithOptions(bgImage.size, NO, 0.0);10     11     // 2.画背景12     [bgImage drawInRect:CGRectMake(0, 0, bgImage.size.width, bgImage.size.height)];13     14     // 3.画右下角的水印15     UIImage *waterImage = [UIImage imageNamed:logo];16     CGFloat scale = 0.2;17     CGFloat margin = 5;18     CGFloat waterW = waterImage.size.width * scale;19     CGFloat waterH = waterImage.size.height * scale;20     CGFloat waterX = bgImage.size.width - waterW - margin;21     CGFloat waterY = bgImage.size.height - waterH - margin;22     [waterImage drawInRect:CGRectMake(waterX, waterY, waterW, waterH)];23     24     // 4.从上下文中取得制作完毕的UIImage对象25     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();26     27     // 5.结束上下文28     UIGraphicsEndImageContext();29     30     return newImage;31 }32 @end

图片裁剪
 1 #import "UIImage+ZR.h" 2  3 @implementation UIImage (ZR) 4 + (instancetype)circleImageWithName:(NSString *)name borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor 5 { 6     // 1.加载原图 7     UIImage *oldImage = [UIImage imageNamed:name]; 8      9     // 2.开启上下文10     CGFloat imageW = oldImage.size.width + 2 * borderWidth;11     CGFloat imageH = oldImage.size.height + 2 * borderWidth;12     CGSize imageSize = CGSizeMake(imageW, imageH);13     UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);14     15     // 3.取得当前的上下文16     CGContextRef ctx = UIGraphicsGetCurrentContext();17     18     // 4.画边框(大圆)19     [borderColor set];20     CGFloat bigRadius = imageW * 0.5; // 大圆半径21     CGFloat centerX = bigRadius; // 圆心22     CGFloat centerY = bigRadius;23     CGContextAddArc(ctx, centerX, centerY, bigRadius, 0, M_PI * 2, 0);24     CGContextFillPath(ctx); // 画圆25     26     // 5.小圆27     CGFloat smallRadius = bigRadius - borderWidth;28     CGContextAddArc(ctx, centerX, centerY, smallRadius, 0, M_PI * 2, 0);29     // 裁剪(后面画的东西才会受裁剪的影响)30     CGContextClip(ctx);31     32     // 6.画图33     [oldImage drawInRect:CGRectMake(borderWidth, borderWidth, oldImage.size.width, oldImage.size.height)];34     35     // 7.取图36     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();37     38     // 8.结束上下文39     UIGraphicsEndImageContext();40     41     return newImage;42 }43 @end

 

屏幕截图

 1 #import "UIImage+ZR.h" 2  3 @implementation UIImage (ZR) 4 + (instancetype)captureWithView:(UIView *)view 5 { 6     // 1.开启上下文 7     UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0.0); 8      9     // 2.将控制器view的layer渲染到上下文10     [view.layer renderInContext:UIGraphicsGetCurrentContext()];11     12     // 3.取出图片13     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();14     15     // 4.结束上下文16     UIGraphicsEndImageContext();17     18     return newImage;19 }20 @end

 

背景拉伸

 1 - (void)imageBg 2 { 3     UIImage *oldImage = [UIImage imageNamed:@"me"]; 4      5     UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0.0); 6     [oldImage drawInRect:self.view.bounds]; 7     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 8     UIGraphicsEndImageContext(); 9     10     self.view.backgroundColor = [UIColor colorWithPatternImage:newImage];11 }

 

背景平铺

 1 - (void)viewDidLoad 2 { 3     [super viewDidLoad]; 4      5 //    self.view.backgroundColor = [UIColor redColor]; 6      7     // 1.创建一行背景图片 8     CGFloat rowW = self.view.frame.size.width; 9 //    CGFloat rowH = 40;10     CGFloat rowH = 30;11     UIGraphicsBeginImageContextWithOptions(CGSizeMake(rowW, rowH), NO, 0.0);12     13     CGContextRef ctx = UIGraphicsGetCurrentContext();14     // 画矩形框15     [[UIColor redColor] set];16     CGContextAddRect(ctx, CGRectMake(0, 0, rowW, rowH));17     CGContextFillPath(ctx);18     19     // 2.画线20     [[UIColor greenColor] set];21     CGFloat lineWidth = 2;22     CGContextSetLineWidth(ctx, lineWidth);23     CGFloat dividerX = 0;24     CGFloat dividerY = rowH - lineWidth;25     CGContextMoveToPoint(ctx, dividerX, dividerY);26     CGContextAddLineToPoint(ctx, rowW - dividerX, dividerY);27     CGContextStrokePath(ctx);28     29     // 3.取图30     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();31     32     // 4.结束上下文33     UIGraphicsEndImageContext();34     35     // 5.设置为背景36     self.textView.backgroundColor = [UIColor colorWithPatternImage:newImage];37 }

 

 

 

iOS UIImage 图片水印,图片裁剪,屏幕截图,背景平铺