美文网首页
图片添加logo和文字水印

图片添加logo和文字水印

作者: Luyc_Han | 来源:发表于2019-05-24 12:00 被阅读0次
    //
    //  ViewController.m
    //  WeaterImage
    //
    //  Created by 韩小杰 on 2019/5/24.
    //  Copyright © 2019 韩小杰. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()
    @property (nonatomic,strong) UIImageView *imgView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.imgView = [[UIImageView alloc] initWithFrame:self.view.frame];
        [self.view addSubview:self.imgView];
        
        UIImage *img = [UIImage imageNamed:@"logo"];
    
        UIImage *logo = [UIImage imageNamed:@"需要添加水印的图片"];
        self.imgView.image = [self waterMarkImage:img login:logo title:@"@王木木"];
        
        UIImageWriteToSavedPhotosAlbum(self.imgView.image, self, @selector(completedWithImage:error:context:), NULL);
    }
    
    -(UIImage *)waterMarkImage:(UIImage *)image login:(UIImage *)login title:(NSString *)title{
        
        CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
        CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
        
        CGFloat W = image.size.width;
        CGFloat H = image.size.height;
        
        CGFloat correctH = 0;
        if(W >= H){
            correctH = 15;
        }else{
            correctH = 0;
        }
        
        CGFloat Font = 13;
        
        CGFloat wC = W/screenW;
        CGFloat hC = H/screenH;
        
        UIGraphicsBeginImageContext(image.size);
        [image drawInRect:CGRectMake(0, 0, W, H)];
        
        Font = Font * wC;
    
        CGRect postionIconF = [self getCgrect:18 y:615 w:16 h:20 wC:wC hC:hC];
        CGRect postionStrF = [self getCgrect:40 y:615 w:200 h:40 wC:wC hC:hC];
        
        
        CGSize size = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, postionStrF.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:Font]} context:nil].size;
        
        CGFloat screenHeight = image.size.height - postionIconF.size.height;
        
        CGFloat postionIconFx = (image.size.width - postionIconF.size.width - size.width - 10) /2;
        
        postionIconF = CGRectMake(postionIconFx, screenHeight, postionIconF.size.width, postionIconF.size.height);
        
        CGFloat postionStrFx = postionIconF.origin.x + postionIconF.size.width + 10;
        postionStrF = CGRectMake(postionStrFx, screenHeight, size.width + 20, postionStrF.size.height);
        
        UIImage *loginIcon = login;
        [loginIcon drawInRect:CGRectMake(postionIconF.origin.x, postionIconF.origin.y, postionIconF.size.width, postionIconF.size.width)];
        
        NSString *titleStr = title;
        NSDictionary *positionAttr = @{
                                       NSFontAttributeName: [UIFont boldSystemFontOfSize:Font],
                                       NSForegroundColorAttributeName : [UIColor redColor]
                                       };
        [titleStr drawInRect:postionStrF withAttributes:positionAttr];
        
        UIImage *imageEnd = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return imageEnd;
    }
    
    - (CGRect)getCgrect:(CGFloat)x y:(CGFloat)y w:(CGFloat)w h:(CGFloat)h wC:(CGFloat)wC hC:(CGFloat)hC{
        CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
        CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
        
        return CGRectMake(x * screenW/375.0 * wC, y*screenH/667.0 * hC, w * wC, h * wC);
    }
    
    - (void)completedWithImage:(UIImage *)image error:(NSError *)error context:(void *)context {
        if (error) {
            NSLog(@"failure");
        } else {
            NSLog(@"successful");
        }
    }
    @end
    
    
    WechatIMG68.jpeg

    相关文章

      网友评论

          本文标题:图片添加logo和文字水印

          本文链接:https://www.haomeiwen.com/subject/xakgzqtx.html