• iOS 图片水印、图片合成文字或图片实现


    这个需求可能有时候会碰到,比如自己的照片加版权,打水印等

    网上的方法,有不少感觉不全对,或者需求不是特全,这里我总结了3种场景下的需求:

    1、本地图片合成文字

    2、本地图片合成图片

    3、网络图片先下载再合成图片

    效果图:

    这里的合成的size大小,我都是随便写的,没特意计算,大家可以按实际需求自定义。

    代码部分:

    /**
     图片合成文字
    
     @param img <#img description#>
     @param logoText <#logoText description#>
     @return <#return value description#>
     */
    - (UIImage *)imageAddText:(UIImage *)img text:(NSString *)logoText
    {
        NSString* mark = logoText;
        int w = img.size.width;
        int h = img.size.height;
        UIGraphicsBeginImageContext(img.size);
        [img drawInRect:CGRectMake(0, 0, w, h)];
        NSDictionary *attr = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:55], NSForegroundColorAttributeName : [UIColor redColor]  };
        //位置显示
        [mark drawInRect:CGRectMake(10, 20, w*0.8, h*0.3) withAttributes:attr];
        
        UIImage *aimg = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();  
        
        return aimg;
        
    }
    /**
     本地图片合成
    
     @param useImage <#useImage description#>
     @param maskImage <#maskImage description#>
     @return <#return value description#>
     */
    - (UIImage *)imageAddLocalImage:(UIImage *)useImage addMsakImage:(UIImage *)maskImage
    {
        
        UIGraphicsBeginImageContextWithOptions(useImage.size ,NO, 0.0);
        [useImage drawInRect:CGRectMake(0, 0, useImage.size.width, useImage.size.height)];
        
        //四个参数为水印图片的位置
        [maskImage drawInRect:CGRectMake(0, 0, useImage.size.width, useImage.size.height/2)];
        //如果要多个位置显示,继续drawInRect就行
        //[maskImage drawInRect:CGRectMake(0, useImage.size.height/2, useImage.size.width, useImage.size.height/2)];
        UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return resultingImage;
    }
    /**
     下载网络图片合成
    
     @param imgUrl <#imgUrl description#>
     @param imgUrl2 <#imgUrl2 description#>
     @param imgView <#imgView description#>
     */
    - (void)imageAddUrlImage:(NSString *)imgUrl image2:(NSString *)imgUrl2 showinImageView:(UIImageView *)imgView
    {
        // 1.队列组、全局并发队列 的初始化
        dispatch_group_t group = dispatch_group_create();
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        
        // 2.在block内部不能修改外部的局部变量,这里必须要加前缀 __block
        __block UIImage *image1 = nil;
        
        // 注意这里的异步执行方法多了一个group(队列)
        dispatch_group_async(group, queue, ^{
            NSURL *url1 = [NSURL URLWithString:imgUrl];
            NSData *data1 = [NSData dataWithContentsOfURL:url1];
            image1 = [UIImage imageWithData:data1];
        });
        
        // 3.下载图片2
        __block UIImage *image2 = nil;
        dispatch_group_async(group, queue, ^{
            NSURL *url2 = [NSURL URLWithString:imgUrl2];
            NSData *data2 = [NSData dataWithContentsOfURL:url2];
            image2 = [UIImage imageWithData:data2];
        });
        
        __block UIImage *fullImage;
        // 4.合并图片 (保证执行完组里面的所有任务之后,再执行notify函数里面的block)
        dispatch_group_notify(group, queue, ^{
            
            UIGraphicsBeginImageContextWithOptions(image1.size ,NO, 0.0);
            [image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];
            [image2 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height/2)];
            fullImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            
            
            dispatch_async(dispatch_get_main_queue(), ^{
                imgView.image = fullImage;
            });
        });
    }

    注意:上面的合成位置,都是我随便写的,实际场景下,大家可以自己按需求定义,或将位置传参也行,楼主是因为偷懒来着。。。

  • 相关阅读:
    I4-6 Sports and Extreme Sports Teacher:Lamb
    English trip V2-B 24 Biographies Teacher: TAALAN
    git设置下载代理 http or socket5 设置方法
    The "Go" Learning Trip -- 0. Base build
    English trip V2-B 23 Making a Good Impression Teacher: GABRIELE
    English trip V2-B 22 Tell a Story Teacher: Russell
    English trip V2-B 21 Personal History Teacher: Russell
    English trip EM2- PE 5A COMMUNICATION 交流 Teacher:Corrine
    Phonics 自然拼读法 ou ow oi oy au aw oo oo ea Teacher:Lamb
    beta分布 java代码
  • 原文地址:https://www.cnblogs.com/yajunLi/p/6346736.html
Copyright © 2020-2023  润新知