iOS截屏、给图片添加水印

来源:互联网 发布:多益网络 传送门骑士 编辑:程序博客网 时间:2024/06/14 09:54

//截取视图的内容保存为图片,保存到系统相册

- (IBAction)cutPicAndSaveBtnAction:(id)sender {

    

    //SCALE 为缩放倍数

    UIGraphicsBeginImageContextWithOptions(self.bkView.frame.size,NO,SCALE);

    CGContextRef context =UIGraphicsGetCurrentContext();

    //剪切指定视图的画面

    [self.bkView.layerrenderInContext:context];

    UIImage * cutImage =UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    

    //给图片添加文字水印

    NSDictionary *attributedDic =@{NSFontAttributeName:[UIFontsystemFontOfSize:10],NSForegroundColorAttributeName:[UIColorwhiteColor],NSBackgroundColorAttributeName:[UIColorblackColor]};

    cutImage = [selfaddWaterTextWithImage:cutImagetext:@"身高:170"textPoint:CGPointMake(0,0)attributedString:attributedDic];


     //保存到系统相册

    UIImageWriteToSavedPhotosAlbum(cutImage, self, @selector(image:didFinishSavingWithError:contextInfo:),nil);

}


//给图片添加文字水印

- (UIImage *)addWaterTextWithImage:(UIImage *)image text:(NSString *)text textPoint:(CGPoint)point attributedString:(NSDictionary * )attributed{

    

    //1.开启上下文

    UIGraphicsBeginImageContextWithOptions(image.size,NO,SCALE);

    //2.绘制图片

    [image drawInRect:CGRectMake(0,0, image.size.width, image.size.height)];

    //添加水印文字

    [text drawAtPoint:point withAttributes:attributed];

    //3.从上下文中获取新图片

    UIImage * newImage =UIGraphicsGetImageFromCurrentImageContext();

    //4.关闭图形上下文

    UIGraphicsEndImageContext();

    //返回图片

    return newImage;

}

原创粉丝点击