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參数说明文章

  • 相关阅读:
    session和cookie
    数据库备份
    使用pip安装.whl文件时出现is not a supported wheel on this platform的解决办法
    multiprocessing模块
    threading模块
    python中多线程相关
    python中实现单例模式
    Flask-SQLAlchemy相关与Flask-Migrate相关
    redis模块
    Flask-Login中装饰器@login_manager.user_loader的作用及原理
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/7283159.html
Copyright © 2011-2022 走看看