ios 給圖片上加水印的方法

分享類型:應用開發相關

ios圖片加水印或文字

詳情可見附件,我親自測試了一下,發現原來的方法確實存在諸多問題,重新寫了一下,大家隨意取用,有問題再交流

1.加文字

- (UIImage *)imageWithLogoText:(UIImage *)img text:(NSString*)text1

{

/////注:此為后來更改,用于顯示中文。zyq,2013-5-8

CGSize size = CGSizeMake(200, img.size.height);//設置上下文(畫布)大小

UIGraphicsBeginImageContext(size);//創建一個基于位圖的上下文(context),并將其設置為當前上下文

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:16];

[text1 drawInRect:CGRectMake(0, 0, 200, 80) withFont:font];//此處設置文字顯示的位置

UIImage *targetimg =UIGraphicsGetImageFromCurrentImageContext();//從當前上下文種獲取圖片

UIGraphicsEndImageContext();//移除棧頂的基于當前位圖的圖形上下文。

returntargetimg;

//注:此為原來,不能顯示中文。無用。

//get image width and height

intw = img.size.width;

inth = 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([selfsize],NO, 0.0);// 0.0 for scale means "scale for device's main screen".

}

#else

if([[[UIDevice currentDevice] systemVersion] floatValue] < 4.0)

{

UIGraphicsBeginImageContext([selfsize]);

}

#endif

//原圖

[selfdrawInRect:CGRectMake(0, 0,self.size.width,self.size.height)];

//文字顏色

[color set];

//水印文字

[markString drawInRect:rect withFont:font];

UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

returnnewPic;

}

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([selfsize],NO, 0.0);// 0.0 for scale means "scale for device's main screen".

}

#else

if([[[UIDevice currentDevice] systemVersion] floatValue] < 4.0)

{

UIGraphicsBeginImageContext([selfsize]);

}

#endif

//原圖

[selfdrawInRect:CGRectMake(0, 0,self.size.width,self.size.height)];

//文字顏色

[color set];

//水印文字

[markString drawAtPoint:point withFont:font];

UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

returnnewPic;

}

4.加圖片

#pragma mark - 加圖片水印

-(UIImage *)imageWithLogoImage:(UIImage *)img logo:(UIImage *)logo

{

//get image width and height

intw = img.size.width;

inth = img.size.height;

intlogoWidth = logo.size.width;

intlogoHeight = 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([selfsize],NO, 0.0);// 0.0 for scale means "scale for device's main screen".

}

#else

if([[[UIDevice currentDevice] systemVersion] floatValue] < 4.0)

{

UIGraphicsBeginImageContext([selfsize]);

}

#endif

//原圖

[selfdrawInRect:CGRectMake(0, 0,self.size.width,self.size.height)];

//水印圖

[mask drawInRect:rect];

UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

returnnewPic;

}

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

returnresultingImage;

}

下載的工具類:https://app.yinxiang.com/shard/s70/nl/2147483647/dda44123-65db-4b33-a69d-7c4b4dbf0575/

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容