圖片加水印的多種方式

1.加文字
- (UIImage *)imageWithLogoText:(UIImage *)img text:(NSString *)text1 
{      
    /////注:此為后來更改硅卢,用于顯示中文箩言。zyq,2013-5-8 
    CGSize size = CGSizeMake(200, img.size.height);          //設置上下文(畫布)大小 
    UIGraphicsBeginImageContext(size);                       //創(chuàng)建一個基于位圖的上下文(context)泉唁,并將其設置為當前上下文 
    CGContextRef contextRef = UIGraphicsGetCurrentContext(); //獲取當前上下文 
    CGContextTranslateCTM(contextRef, 0, img.size.height);   //畫布的高度 
    CGContextScaleCTM(contextRef, 1.0, -1.0);                //畫布翻轉(zhuǎn) 
    CGContextDrawImage(contextRef, CGRectMake(0, 0, img.size.width, img.size.height), [img CGImage]);  //在上下文種畫當前圖片 
     
    [[UIColor redColor] set];                                //上下文種的文字屬性 
    CGContextTranslateCTM(contextRef, 0, img.size.height); 
    CGContextScaleCTM(contextRef, 1.0, -1.0); 
    UIFont *font = [UIFont boldSystemFontOfSize:16]; 
    [text1 drawInRect:CGRectMake(0, 0, 200, 80) withFont:font];       //此處設置文字顯示的位置 
    UIImage *targetimg =UIGraphicsGetImageFromCurrentImageContext();  //從當前上下文種獲取圖片 
    UIGraphicsEndImageContext();                            //移除棧頂?shù)幕诋斍拔粓D的圖形上下文郊供。 
    return targetimg; 
     
     
    //注:此為原來,不能顯示中文蛾茉。無用垦江。 
    //get image width and height 
    int w = img.size.width; 
    int h = img.size.height; 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    //create a graphic context with CGBitmapContextCreate 
    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst); 
    CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage); 
    CGContextSetRGBFillColor(context, 0.0, 1.0, 1.0, 1); 
    char* text = (char *)[text1 cStringUsingEncoding:NSUnicodeStringEncoding]; 
    CGContextSelectFont(context, "Georgia", 30, kCGEncodingMacRoman); 
    CGContextSetTextDrawingMode(context, kCGTextFill); 
    CGContextSetRGBFillColor(context, 255, 0, 0, 1); 
    CGContextShowTextAtPoint(context, w/2-strlen(text)*5, h/2, text, strlen(text)); 
    //Create image ref from the context 
    CGImageRef imageMasked = CGBitmapContextCreateImage(context); 
    CGContextRelease(context); 
    CGColorSpaceRelease(colorSpace); 
    return [UIImage imageWithCGImage:imageMasked]; 
     
} 
2.新的添加文字水印的方法
#pragma mark -也是文字水印 
- (UIImage *) imageWithStringWaterMark:(NSString *)markString inRect:(CGRect)rect color:(UIColor *)color font:(UIFont *)font 
{ 
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000 
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0) 
    { 
        UIGraphicsBeginImageContextWithOptions([self size], NO, 0.0); // 0.0 for scale means "scale for device's main screen". 
    } 
#else 
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 4.0) 
    { 
        UIGraphicsBeginImageContext([self size]); 
    } 
#endif 
     
    //原圖 
    [self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)]; 
     
    //文字顏色 
    [color set]; 
     
    //水印文字 
    [markString drawInRect:rect withFont:font]; 
     
    UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
     
    return newPic; 
} 
3.還是添加文字水印方法二
#pragma mark -還是文字水印 
- (UIImage *) imageWithStringWaterMark:(NSString *)markString atPoint:(CGPoint)point color:(UIColor *)color font:(UIFont *)font 
{ 
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000 
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0) 
    { 
        UIGraphicsBeginImageContextWithOptions([self size], NO, 0.0); // 0.0 for scale means "scale for device's main screen". 
    } 
#else 
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 4.0) 
    { 
        UIGraphicsBeginImageContext([self size]); 
    } 
#endif 
     
    //原圖 
    [self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)]; 
     
    //文字顏色 
    [color set]; 
     
    //水印文字 
    [markString drawAtPoint:point withFont:font]; 
     
    UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
     
    return newPic; 
} 
4,加圖片
#pragma mark - 加圖片水印 
-(UIImage *)imageWithLogoImage:(UIImage *)img logo:(UIImage *)logo 
{ 
    //get image width and height 
    int w = img.size.width; 
    int h = img.size.height; 
    int logoWidth = logo.size.width; 
    int logoHeight = logo.size.height; 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    //create a graphic context with CGBitmapContextCreate 
    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst); 
    CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage); 
    CGContextDrawImage(context, CGRectMake(w-logoWidth, 0, logoWidth, logoHeight), [logo CGImage]); 
    CGImageRef imageMasked = CGBitmapContextCreateImage(context); 
    CGContextRelease(context); 
    CGColorSpaceRelease(colorSpace); 
    return [UIImage imageWithCGImage:imageMasked]; 
    //  CGContextDrawImage(contextRef, CGRectMake(100, 50, 200, 80), [smallImg CGImage]); 
} 

5.新的添加圖片水印的方法
#pragma mark -還是圖片水印 
- (UIImage *) imageWithWaterMask:(UIImage*)mask inRect:(CGRect)rect 
{ 
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000 
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0) 
    { 
        UIGraphicsBeginImageContextWithOptions([self size], NO, 0.0); // 0.0 for scale means "scale for device's main screen". 
    } 
#else 
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 4.0) 
    { 
        UIGraphicsBeginImageContext([self size]); 
    } 
#endif 
     
    //原圖 
    [self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)]; 
    //水印圖 
    [mask drawInRect:rect]; 
     
    UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
     
    return newPic; 
} 
6。加半透明的水印
//加半透明的水印 
-(UIImage *)imageWithTransImage:(UIImage *)useImage addtransparentImage:(UIImage *)transparentimg 
{ 
    UIGraphicsBeginImageContext(useImage.size); 
    [useImage drawInRect:CGRectMake(0, 0, useImage.size.width, useImage.size.height)]; 
    [transparentimg drawInRect:CGRectMake(0, useImage.size.height-transparentimg.size.height, transparentimg.size.width, transparentimg.size.height) blendMode:kCGBlendModeOverlay alpha:0.4f]; 
     
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return resultingImage; 
}

參考鏈接

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末棠涮,一起剝皮案震驚了整個濱河市谬哀,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌严肪,老刑警劉巖史煎,帶你破解...
    沈念sama閱讀 221,198評論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異驳糯,居然都是意外死亡篇梭,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評論 3 398
  • 文/潘曉璐 我一進店門结窘,熙熙樓的掌柜王于貴愁眉苦臉地迎上來很洋,“玉大人,你說我怎么就攤上這事隧枫『泶牛” “怎么了?”我有些...
    開封第一講書人閱讀 167,643評論 0 360
  • 文/不壞的土叔 我叫張陵官脓,是天一觀的道長协怒。 經(jīng)常有香客問我,道長卑笨,這世上最難降的妖魔是什么孕暇? 我笑而不...
    開封第一講書人閱讀 59,495評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上妖滔,老公的妹妹穿的比我還像新娘隧哮。我一直安慰自己,他們只是感情好座舍,可當我...
    茶點故事閱讀 68,502評論 6 397
  • 文/花漫 我一把揭開白布沮翔。 她就那樣靜靜地躺著,像睡著了一般曲秉。 火紅的嫁衣襯著肌膚如雪采蚀。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,156評論 1 308
  • 那天承二,我揣著相機與錄音榆鼠,去河邊找鬼。 笑死亥鸠,一個胖子當著我的面吹牛妆够,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播负蚊,決...
    沈念sama閱讀 40,743評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼责静,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了盖桥?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,659評論 0 276
  • 序言:老撾萬榮一對情侶失蹤题翻,失蹤者是張志新(化名)和其女友劉穎揩徊,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體嵌赠,經(jīng)...
    沈念sama閱讀 46,200評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡塑荒,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,282評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了姜挺。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片齿税。...
    茶點故事閱讀 40,424評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖炊豪,靈堂內(nèi)的尸體忽然破棺而出凌箕,到底是詐尸還是另有隱情,我是刑警寧澤词渤,帶...
    沈念sama閱讀 36,107評論 5 349
  • 正文 年R本政府宣布牵舱,位于F島的核電站,受9級特大地震影響缺虐,放射性物質(zhì)發(fā)生泄漏芜壁。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,789評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望慧妄。 院中可真熱鬧顷牌,春花似錦、人聲如沸塞淹。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽窖铡。三九已至疗锐,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間费彼,已是汗流浹背滑臊。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評論 1 271
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留箍铲,地道東北人雇卷。 一個月前我還...
    沈念sama閱讀 48,798評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像颠猴,于是被迫代替她去往敵國和親关划。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,435評論 2 359

推薦閱讀更多精彩內(nèi)容