美文网首页
给照片添加水印

给照片添加水印

作者: 信徒阿门 | 来源:发表于2017-10-19 14:20 被阅读0次

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

}

#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;

CGColorSpaceRefcolorSpace =CGColorSpaceCreateDeviceRGB();

//create a graphic context with CGBitmapContextCreate

CGContextRefcontext =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), [logoCGImage]);

CGImageRefimageMasked =CGBitmapContextCreateImage(context);

CGContextRelease(context);

CGColorSpaceRelease(colorSpace);

return[UIImageimageWithCGImage:imageMasked];

//CGContextDrawImage(contextRef, CGRectMake(100, 50, 200, 80), [smallImg CGImage]);

}

#pragma mark -还是图片水印

- (UIImage*) imageWithWaterMask:(UIImage*)mask inRect:(CGRect)rect

{

#if __IPHONE_OS_VERSION_MAX_ALLOWED >=40000

if([[[UIDevicecurrentDevice]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)];

//水印图

[maskdrawInRect:rect];

UIImage*newPic =UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

returnnewPic;

}

//加半透明的水印

-(UIImage*)imageWithTransImage:(UIImage*)useImage addtransparentImage:(UIImage*)transparentimg

{

UIGraphicsBeginImageContext(useImage.size);

[useImagedrawInRect:CGRectMake(0,0, useImage.size.width, useImage.size.height)];

[transparentimgdrawInRect:CGRectMake(0, useImage.size.height-transparentimg.size.height, transparentimg.size.width, transparentimg.size.height)blendMode:kCGBlendModeOverlayalpha:0.4f];

UIImage*resultingImage =UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

returnresultingImage;

}

相关文章

网友评论

      本文标题:给照片添加水印

      本文链接:https://www.haomeiwen.com/subject/nqvuuxtx.html