iOS 圖片浮水印、圖片合成文字或圖片實現

來源:互聯網
上載者:User

標籤:dispatch   not   函數   creat   自己   val   div   變數   str   

這個需求可能有時候會碰到,比如自己的照片加著作權,打浮水印等

網上的方法,有不少感覺不全對,或者需求不是特全,這裡我總結了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;        });    });}

 

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

 

iOS 圖片浮水印、圖片合成文字或圖片實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.