给照片添加水印

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

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容