浮水印的實作原理就是「開一個跟原圖同尺寸的位圖上下文,把原圖畫進去,再把文字或 logo 疊上去,最後從上下文取出一張新圖」。以下提供三個可直接複用的方法。
+ (instancetype)waterImageWithText:(UIImage *)img
textLogoColor:(NSString *)text1 {
CGSize size = CGSizeMake(img.size.width, img.size.height);
// 建立基於位圖的上下文並設為當前上下文
UIGraphicsBeginImageContext(size);
CGContextRef contextRef = UIGraphicsGetCurrentContext();
// 畫布翻轉:Core Graphics 原點在左下,UIKit 在左上,
// 這裡做 Y 軸翻轉讓兩套座標系對齊
CGContextTranslateCTM(contextRef, 0, img.size.height);
CGContextScaleCTM(contextRef, 1.0, -1.0);
// 畫原圖
CGContextDrawImage(contextRef,
CGRectMake(0, 0, img.size.width, img.size.height),
[img CGImage]);
[[UIColor whiteColor] set];
// 文字繪製同樣需要翻回來,否則會上下顛倒
CGContextTranslateCTM(contextRef, 0, img.size.height);
CGContextScaleCTM(contextRef, 1.0, -1.0);
UIFont *font = [UIFont boldSystemFontOfSize:30];
// 文字位置:靠左下角,距底部 40pt
[text1 drawInRect:CGRectMake(120, img.size.height - 40,
img.size.width - 80, 40)
withFont:font];
UIImage *targetimg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); // 務必關閉,否則記憶體不會釋放
return targetimg;
}
這種寫法不必手動處理矩陣,直接用 UIKit 的 drawInRect: 就好,代碼更短也更不容易出錯。
+ (instancetype)waterImageWithImage:(UIImage *)bgImage
logo:(NSString *)logo {
// 1. 建立基於位圖的上下文
UIGraphicsBeginImageContextWithOptions(bgImage.size, NO, 0.0);
// 2. 畫背景圖
[bgImage drawInRect:CGRectMake(0, 0,
bgImage.size.width, bgImage.size.height)];
// 3. 計算浮水印位置:左下角,留 5pt 邊距
UIImage *waterImage = [UIImage imageNamed:logo];
CGFloat scale = 1;
CGFloat margin = 5;
CGFloat waterW = waterImage.size.width * scale;
CGFloat waterH = waterImage.size.height * scale;
CGFloat waterX = 0;
CGFloat waterY = bgImage.size.height - waterH - margin;
[waterImage drawInRect:CGRectMake(waterX, waterY, waterW, waterH)];
// 4. 從上下文取出成品
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
// 5. 關閉上下文
UIGraphicsEndImageContext();
return newImage;
}
+ (instancetype)waterImageWithText:(UIImage *)img
textLogo:(NSString *)text1 {
CGSize size = CGSizeMake(img.size.width, img.size.height);
UIGraphicsBeginImageContext(size);
CGContextRef contextRef = UIGraphicsGetCurrentContext();
// 矩陣轉換:移動 + 縮放
CGContextTranslateCTM(contextRef, 0, img.size.height);
CGContextScaleCTM(contextRef, 1.0, -1.0);
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:30];
// 用 withAttributes: 可以帶入顏色、陰影等更豐富的樣式
[text1 drawInRect:CGRectMake(120, img.size.height - 40,
img.size.width - 80, 40)
withAttributes:@{ NSFontAttributeName: font }];
UIImage *targetimg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return targetimg;
}
drawInRect:withFont: 在 iOS 7 已被標記為棄用,新專案請改用 drawInRect:withAttributes:,並在字典中帶入 NSForegroundColorAttributeName、NSShadowAttributeName 等設定。UIGraphicsBeginImageContextWithOptions 的第三個參數傳 0 表示沿用螢幕 scale,在 Retina 裝置上會產出清晰圖;若傳 1.0 則會強制輸出 1x,畫質明顯變差。drawInRect: 會自動換行,但要搭配 NSParagraphStyleAttributeName 設定行距與對齊才美觀。CGImageSource 建立縮圖後再疊水印。