IOS:UIImage的剪切,尺寸缩小、压缩、添加水印
不多说了,直接贴代码吧
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:@"portrait01.png"];
CGFloat width = image.size.width;
CGFloat height = image.size.height;
//加图片水印
UIImage *image01 = [self addToImage:image image:image withRect:CGRectMake(0, 20, 30, 30)];
UIImageView *imag = [[UIImageView alloc] initWithImage:image01];
imag.frame = CGRectMake(10, 100, width,height);
[self.view addSubview:imag];
//剪切图片
UIImage *image1 =[self cutImage:image withRect:CGRectMake(10, 20, 60, 100)];//
int w = image1.size.width;
int h = image1.size.height;
UIImage *image11 = [self addText:image1 text:@"剪切" withRect:CGRectMake(10,(h-30)/2, w, 30) ];
UIImageView *imag1 = [[UIImageView alloc] initWithImage:image11];
imag1.frame = CGRectMake(10, 210, image1.size.width,image1.size.height);
[self.view addSubview:imag1];
//缩小图片
UIImage *image2 = [self scaleToSize:image size:CGSizeMake(image1.size.width, image1.size.height)];
UIImage *image22 = [self addText:image2 text:@"压缩" withRect:CGRectMake(10,(h-30)/2, w, 30) ];
UIImageView *imag2 = [[UIImageView alloc] initWithImage:image22];
imag2.frame = CGRectMake(10, 300, image2.size.width,image2.size.height);
[self.view addSubview:imag2];
//压缩图片大小并保存
[self zipImageData:image];
}
//压缩图片
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
// 设置成为当前正在使用的context
UIGraphicsBeginImageContext(size);
// 绘制改变大小的图片
[img drawInRect:CGRectMake(0, 0, size.width, size.height)];
// 从当前context中创建一个改变大小后的图片
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
// 使当前的context出堆栈
UIGraphicsEndImageContext();
// 返回新的改变大小后的图片
return scaledImage;
}
//截图图片
- (UIImage *)cutImage:(UIImage *)image withRect:(CGRect )rect
{
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
UIImage * img = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return img;
}
//压缩图片大小
- (void)zipImageData:(UIImage *)image
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyyMMddHHSSS"];
NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];
NSString *dateStr = [NSString stringWithFormat:@"%@.jpg",currentDateStr];
NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:dateStr];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
NSError *error;
[[NSFileManager defaultManager] removeItemAtPath:path error:&error];
}
NSData *imgData = UIImageJPEGRepresentation(image, 1);
if([imgData writeToFile:path atomically:YES])
{
NSLog(@"saveSuccess");
}
}
//加文字水印
- (UIImage *) addText:(UIImage *)img text:(NSString *)mark withRect:(CGRect)rect
{
int w = img.size.width;
int h = img.size.height;
UIGraphicsBeginImageContext(img.size);
[[UIColor redColor] set];
[img drawInRect:CGRectMake(0, 0, w, h)];
if([[[UIDevice currentDevice]systemName]floatValue] >= 7.0)
{
NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:20.0f], NSFontAttributeName,[UIColor blueColor] ,NSForegroundColorAttributeName,nil];
[mark drawInRect:rect withAttributes:dic];
}
else
{
//该方法在7.0及其以后都废弃了
[mark drawInRect:rect withFont:[UIFont systemFontOfSize:20]];
}
UIImage *aimg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return aimg;
}
//加图片水印
- (UIImage *) addToImage:(UIImage *)img image:(UIImage *)newImage withRect:(CGRect)rect
{
int w = img.size.width;
int h = img.size.height;
UIGraphicsBeginImageContext(img.size);
[img drawInRect:CGRectMake(0, 0, w, h)];
[newImage drawInRect:rect];
UIImage *aimg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return aimg;
}
IOS:UIImage的剪切,尺寸缩小、压缩、添加水印相关推荐
- 如何快速在线压缩图片大小(包括放大、缩小图片,指定图片尺寸和压缩图片品质)
在日常的学习和工作中,我们经常会遇到压缩和修改图片大小的问题,如果我们不是专业的设计人员,一般电脑上不会安装ps软件,那么我们如何通过网页,在线快速简单的来完成操作呢?下面小编和大家一块分享下具体如何 ...
- IOS 图片上传处理 图片压缩 图片处理
提到从摄像头/相册获取图片是面向终端用户的,由用户去浏览并选择图片为程序使用.在这里,我们需要过UIImagePickerController类来和用户交互. 使用UIImagePickerContr ...
- IOS 开发 iPhone屏幕尺寸、分辨率及适配
版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 1.iPhone尺寸规格 设备 iPhone 宽 Width 高 Height 对角线 Diagonal 逻辑分辨率(point ...
- ios学习7_iPhone屏幕尺寸、分辨率及适配
1.iPhone尺寸规格 设备 iPhone 宽 Width 高 Height 对角线 Diagonal 逻辑分辨率(point) Scale Factor 设备分辨率(pixel) PPI 3GS ...
- IOS UIImage
UIImage 是一种比较有难度的显示图片的方式,UIImage 可以从文件或者 ImageData 中来显示图片. 如果可以图形化操作,建议使用 ImageView 这个 Control 来显示图片 ...
- 【Android 内存优化】Bitmap 图像尺寸缩小 ( 考虑像素密度、针对从不同像素密度资源中解码对应的 Bitmap 对象 | inDensity | inTargetDensity )
文章目录 一.像素密度对解码图片的影响 二.不考虑像素密度会导致图片缩小尺寸不准确 三.DisplayMetrics 源码阅读.研究手机资源获取规则 四.像素密度参数设置取值 ( inDensity ...
- 【Android 内存优化】Bitmap 图像尺寸缩小 ( 设置 Options 参数 | inJustDecodeBounds | inSampleSize | 工具类实现 )
文章目录 一.解码图片参数 inJustDecodeBounds 二.计算图片的缩小比例 三.设置图片缩小配置 inSampleSize 四.设置图片像素格式 inPreferredConfig 五. ...
- iOS 设备的屏幕尺寸
记录下iOS设备的屏幕尺寸 iPhone 设备: iPhone 1G - 320x480 iPhone 3G - 320x480 iPhone 3GS - 320x480 iPhone 4 - 640 ...
- IOS 判断设备屏幕尺寸、分辨率 根据屏幕尺寸和分辨率
IOS 判断设备屏幕尺寸.分辨率 根据屏幕尺寸和分辨率,ios现在数起来有6个版本. iOS 设备现有的分辨率如下: iPhone/iPod Touch 普通屏 ...
最新文章
- python使用方法-Python的使用方法
- sql 字符串函数(一)
- Docker1.8在Centos7下的安装
- php新闻删除功能设计,php原生开发新闻站之删除新闻
- c#制作发行版 vs_vs2019制作多项目工程模板 - Jackie Hao
- php框架启动过程,框架启动方式 - CrossPHP 框架文档
- atitit。gui 界面皮肤以及换肤总结 java .net c++
- micropython支持stm32型号_轻松几步实现在STM32上运行FreeRTOS任务
- Power Switching ----- Controlling power for power shutoff
- 初学键盘计算机输入时注意,打字练习说明.doc
- 电脑关闭所有应用的快捷键
- Docker学习之二------基础命令(镜像、容器)
- RT-Thread 软件定时器(学习笔记)
- 三菱FX系列PLC模拟量输入AD模块的使用方法和相关编程设置详解
- 【算法竞赛学习笔记】状压DP
- 2020-03-03
- 按一定规律将电文变成密码: 将字母A变为E、将字母a变为e,即变成其后的第四个字母,W将变成A。字母按上述规律转换,非字母字符不变。输入一行字符,输出相应密码。
- 【荤七素八之旅】 跟着吃货知已在一天之内吃透武汉
- 企业数据资产管理平台建设方案(PPT)
- 将销售订单号 + 行号 带到销售出库单上
热门文章
- cas1082287-99-7齐岳生物花菁染料合成线路
- 解决js newDate()苹果手机日期格式显示NaN
- 参考文献怎么找?3分钟找到1000篇论文参考文献!
- 使用OpenCV调用摄像头检测人脸并截图-Python
- 区块链溯源是什么?一文带您读懂!
- 《终结拖延症》读书笔记,作者威廉·克瑙斯
- 简单的汇率转换工具---初试AJAX
- 【IDEA使用教程】IDEA快捷键
- 智慧养老、养老运营服务平台、陪护服务、养老院、托养、敬老院、健康管理、日常护理、残疾人管理、线下援助服务、助餐、助洁、助医、生活照料、养生保健、社区养老、居家养老、服务回访、健康数据、Axure原型
- JAVA 实现阿里云短信申请模板以及批量发送短信