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;
        });
    });
}

 

注意:上面的合成位置,都是我隨便寫的,實際場景下,大家可以自己按需求定義,或將位置傳參也行,樓主是因為偷懶來着。。。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM