iOS图片水印,图片裁剪和屏幕截图

热门标签

,

特别声明:文章多为网络转载,资源使用一般不提供任何帮助,特殊资源除外,如有侵权请联系!

1.图片水印

- (void)getnewImage {
    UIImage *bgImage = [UIImage imageNamed:@""];
    
    //创建一个位图上下文
    UIGraphicsBeginImageContextWithOptions(bgImage.size, NO, 0.0);
    
    //将背景图片画入位图中
    [bgImage drawInRect:CGRectMake(0, 0, bgImage.size.width, bgImage.size.height)];
    
    //将水印Logo画入背景图中
    UIImage *waterIma = [UIImage imageNamed:@""];
    [waterIma drawInRect:CGRectMake(bgImage.size.width - 40 - 5, bgImage.size.height - 40 - 5, 40, 40)];
    
    //取得位图上下文中创建的新的图片
    UIImage *newimage = UIGraphicsGetImageFromCurrentImageContext();
    
    //结束上下文
    UIGraphicsEndImageContext();
    
    //在创建的ImageView上显示出新图片
    self.newImage = newimage;
    
    //压缩新照片为PNG格式的二进制数据
    NSData *data = UIImagePNGRepresentation(newimage);
    
    //将图片写入到手机存储中
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"new.png"];
    [data writeToFile:path atomically:YES];
    
}

2.图片裁剪

//1.加载原图
        UIImage *oldImage = [UIImage imageNamed:@"me"];
 
        //2.获取位图上下文
        CGFloat bigCic = oldImage.size.width + 2 * 2;
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(bigCic, bigCic), NO, 0.0);
 
        //3.画大圆
        [[UIColor whiteColor] set];
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextAddArc(ctx, bigCic * 0.5, bigCic * 0.5, bigCic * 0.5, 0, M_PI * 2, 0);
        CGContextFillPath(ctx);
 
        //4.画小圆
        CGFloat smallCic = oldImage.size.width * 0.5;
        CGContextAddArc(ctx, bigCic * 0.5 , bigCic * 0.5, smallCic, 0, M_PI * 2, 0);
        CGContextClip(ctx);
 
        //5.画图
        [oldImage drawInRect:CGRectMake(2, 2, oldImage.size.width, oldImage.size.height)];
 
        //6.获取新图
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
 
        //7.结束上下文
        UIGraphicsEndImageContext();
 
        //8.显示新图
        self.IconView.image = newImage;
 
        //9.写入到手机存储
        NSData *data = UIImagePNGRepresentation(newImage);
        NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"newClip.png"];
        [data writeToFile:path atomically:YES];

3.屏幕截图

//1.开启位图上下文
UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0.0);
 
//2.渲染截图
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
 
//3.获取新图
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
 
//4.写入到手机存储
NSData *data = UIImagePNGRepresentation(newImage);
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"newClip.png"];
[data writeToFile:path atomically:YES];
 
//5.关闭上下文
UIGraphicsEndImageContext();

 

标签:

未经允许不得转载:作者:SheaYang, 转载或复制请以 超链接形式 并注明出处 技术Dog|博客
原文地址:《iOS图片水印,图片裁剪和屏幕截图》 发布于2019-11-02

分享到:
赞(0) 打赏

评论 抢沙发

9 + 4 =


iOS图片水印,图片裁剪和屏幕截图

长按图片转发给朋友

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

Vieu4.0主题
专业打造轻量级个人企业风格博客主题!专注于前端开发,全站响应式布局自适应模板。

登录

忘记密码 ?