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

  • 相关阅读:
    struts2文件上传大小限制问题
    kindeditor使用方法
    ajaxFileUpload SyntaxError: syntax error
    Attempted to lock an already-locked dir异常解决方法
    Simditor图片上传
    心理学
    Vue.JS
    SQL Server 日常维护经典应用
    12 个 JS 技巧
    Linq to js
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/7283159.html
Copyright © 2011-2022 走看看