美文网首页
图片水印 iOS

图片水印 iOS

作者: 秋叶红90 | 来源:发表于2017-12-25 11:31 被阅读18次

不多说了,直接贴代码吧

- (void)viewDidLoad {

[superviewDidLoad];

UIImage*image = [UIImageimageNamed:@"portrait01.png"];

CGFloatwidth = image.size.width;

CGFloatheight = image.size.height;

//加图片水印

UIImage*image01 = [selfaddToImage:imageimage:imagewithRect:CGRectMake(0,20,30,30)];

UIImageView*imag = [[UIImageViewalloc]initWithImage:image01];

imag.frame=CGRectMake(10,100, width,height);

[self.viewaddSubview:imag];

//剪切图片

UIImage*image1 =[selfcutImage:imagewithRect:CGRectMake(10,20,60,100)];//

intw = image1.size.width;

inth = image1.size.height;

UIImage*image11 = [selfaddText:image1text:@"剪切"withRect:CGRectMake(10,(h-30)/2, w,30) ];

UIImageView*imag1 = [[UIImageViewalloc]initWithImage:image11];

imag1.frame=CGRectMake(10,210, image1.size.width,image1.size.height);

[self.viewaddSubview:imag1];

//缩小图片

UIImage*image2 = [selfscaleToSize:imagesize:CGSizeMake(image1.size.width, image1.size.height)];

UIImage*image22 = [selfaddText:image2text:@"压缩"withRect:CGRectMake(10,(h-30)/2, w,30) ];

UIImageView*imag2 = [[UIImageViewalloc]initWithImage:image22];

imag2.frame=CGRectMake(10,300, image2.size.width,image2.size.height);

[self.viewaddSubview:imag2];

//压缩图片大小并保存

[selfzipImageData:image];

}

//压缩图片

- (UIImage*)scaleToSize:(UIImage*)img size:(CGSize)size{

//设置成为当前正在使用的context

UIGraphicsBeginImageContext(size);

//绘制改变大小的图片

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

//从当前context中创建一个改变大小后的图片

UIImage* scaledImage =UIGraphicsGetImageFromCurrentImageContext();

//使当前的context出堆栈

UIGraphicsEndImageContext();

//返回新的改变大小后的图片

returnscaledImage;

}

//截图图片

- (UIImage*)cutImage:(UIImage*)image withRect:(CGRect)rect

{

CGImageRefimageRef =CGImageCreateWithImageInRect([imageCGImage], rect);

UIImage* img = [UIImageimageWithCGImage:imageRef];

CGImageRelease(imageRef);

returnimg;

}

//压缩图片大小

- (void)zipImageData:(UIImage*)image

{

NSDateFormatter*dateFormatter = [[NSDateFormatteralloc]init];

[dateFormattersetDateFormat:@"yyyyMMddHHSSS"];

NSString*currentDateStr = [dateFormatterstringFromDate:[NSDatedate]];

NSString*dateStr = [NSStringstringWithFormat:@"%@.jpg",currentDateStr];

NSString*path = [NSTemporaryDirectory()stringByAppendingPathComponent:dateStr];

if([[NSFileManagerdefaultManager]fileExistsAtPath:path]) {

NSError*error;

[[NSFileManagerdefaultManager]removeItemAtPath:patherror:&error];

}

NSData*imgData =UIImageJPEGRepresentation(image,1);

if([imgDatawriteToFile:pathatomically:YES])

{

NSLog(@"saveSuccess");

}

}

//加文字水印

- (UIImage*) addText:(UIImage*)img text:(NSString*)mark withRect:(CGRect)rect

{

intw = img.size.width;

inth = img.size.height;

UIGraphicsBeginImageContext(img.size);

[[UIColorredColor]set];

[imgdrawInRect:CGRectMake(0,0, w, h)];

if([[[UIDevicecurrentDevice]systemName]floatValue] >=7.0)

{

NSDictionary* dic = [NSDictionarydictionaryWithObjectsAndKeys:[UIFontsystemFontOfSize:20.0f],NSFontAttributeName,[UIColorblueColor] ,NSForegroundColorAttributeName,nil];

[markdrawInRect:rectwithAttributes:dic];

}

else

{

//该方法在7.0及其以后都废弃了

[markdrawInRect:rectwithFont:[UIFontsystemFontOfSize:20]];

}

UIImage*aimg =UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

returnaimg;

}

//加图片水印

- (UIImage*) addToImage:(UIImage*)img image:(UIImage*)newImage withRect:(CGRect)rect

{

intw = img.size.width;

inth = img.size.height;

UIGraphicsBeginImageContext(img.size);

[imgdrawInRect:CGRectMake(0,0, w, h)];

[newImagedrawInRect:rect];

UIImage*aimg =UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

returnaimg;

}

相关文章

  • iOS如何给图片加水印

    想要给图片加水印其实很简单.只需要如下代码便可轻松搞定。 /**iOS图片加水印 @param useImage ...

  • 图片水印 iOS

    不多说了,直接贴代码吧 - (void)viewDidLoad { [superviewDidLoad]; UII...

  • iOS 图片水印

  • 水印

    图片水印文字 图片水印图片

  • iOS图片添加水印

    iOS添加水印的方法有很多,常用的做法就是直接将水印绘制到图片上。这种方式的缺陷是:如果绘制水印的地方不是纯色,就...

  • js中图片加水印的方法

    因为项目中需要给图片加水印,但是原生实现其他需要安卓和ios两端进行开发,成本有些大,最终选择阿里云的图片加水印方...

  • iOS 自己水印图片

    1、创建一个背景图片UIImage对象 2、创建位图上下文 UIGraphicsBeginImageContext...

  • iOS | 图片打水印

    这个功能前前后后很多细节,也饶了很多弯路,最后核心功能总结如下。 一、添加图片水印(如添加公司logo) 也就是将...

  • iOS图片加水印

    在一些应用如微博中,为了防止用户图片被盗用,一般会在图片上加上水印,接下来就给大家分享一个iOS中给图片加水印的简...

  • iOS 图片加水印

    为图片添加水印文字, 需要先获取图片的图形上下文. 将文字绘制到图形的上下文中, 再根据当前的图形上下文生成一张新...

网友评论

      本文标题:图片水印 iOS

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