zoukankan      html  css  js  c++  java
  • iOS图片加水印效果的实现并保存至相冊


    图片加水印效果的实现并保存至相冊
    实现效果如图:
    效果图


    project下载:githubproject下载链接


    代码:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        UIImage *image = [UIImage imageNamed:@"pushu.jpg"];
        UIImage *waterImage = [self waterMarkImage:image withText:@"朴树水印測试"];
    
        UIImageWriteToSavedPhotosAlbum(waterImage, nil, nil, nil);  //保存图片至相冊
    
    //    展示图片
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
        imageView.image = waterImage;
        [self.view addSubview:imageView];
    
        // Do any additional setup after loading the view, typically from a nib.
    }
    - (UIImage *)waterMarkImage:(UIImage *)image withText:(NSString *)text {
        UIGraphicsBeginImageContext(image.size);
    
    //    在画布中绘制内容
        [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    
    //    绘制文字
        [[UIColor darkGrayColor] set];
        CGRect rect = CGRectMake(70, 220, 200, 60);
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:30],
                              NSObliquenessAttributeName:@1};       //这里设置了字体,和倾斜度。具体其它參数文章结尾有具体说明的文章链接
        [text drawInRect:rect withAttributes:dic];
    
        //在iOS7之前用下列方法比較方便
    //    [text drawInRect:rect withFont:[UIFont systemFontOfSize:30] lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentCenter];
    
    //    从画布中得到image
        UIImage *returnImage = UIGraphicsGetImageFromCurrentImageContext();
    
        UIGraphicsEndImageContext();
    
        return returnImage;
    }

    - (void)drawInRect:(CGRect)rect withAttributes:(NSDictionary *)attrs NS_AVAILABLE_IOS(7_0);

    关于此方法,在iOS7以后应用。attrs设置參数的具体说明在例如以下链接文章中:
    说明文章链接:attrs參数说明文章

  • 相关阅读:
    封装TensorFlow神经网络
    android对话框显示异常报错:You need to use a Theme.AppCompat theme (or descendant) with this activity.
    管道过滤器模式
    架构设计模式之管道——过滤器模式
    SQL SERVER 数据库邮件配置
    浅谈数据仓库的基本架构(转)
    Spark On YARN内存分配
    Tomcat 9.0安装配置
    Spark on Yarn遇到的几个问题
    yarn资源调度(网络搜集)
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/7283159.html
Copyright © 2011-2022 走看看