iOS开发技巧之:UIImage的剪切,尺寸缩小、压缩、添加水印
来源:互联网 发布:单片机串口通讯详解 编辑:程序博客网 时间:2024/05/16 10:56
- (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的剪切,尺寸缩小、压缩、添加水印
- IOS:UIImage的剪切,尺寸缩小、压缩、添加水印
- [转载]UIImage的剪切,尺寸缩小、压缩、添加水印
- IOS:手写签名的实现(实现了手势绘制字体,添加文字水印,图片剪切、图片压缩)
- IOS之UIImage图片的缩小与放大
- iOS 修改UIImage的尺寸
- UIImage 添加文字(水印)
- ios 获取UIImage图片的像素尺寸
- iOS开发中压缩图片的质量以及缩小图片尺寸
- iOS开发技巧之:图片压缩成指定的大小
- ios 之 图片压缩 与剪切
- iOS开发技巧之:图片加水印或文字
- IOS开发基础之UIImage
- iOS 关于UIImage的压缩问题
- iOS 视频剪切、旋转,视频添加音频、添加水印,视频导出
- iOS 视频剪切、旋转,视频添加音频、添加水印,视频导出
- iOS 视频剪切、旋转,视频添加音频、添加水印,视频导出
- iOS 开发之Resize UIimage的三种方法
- String解析->java
- struts2 s2-45漏洞利用
- 蓝桥杯 ADV-176 算法提高 陶陶摘苹果
- XMD
- 打印杨辉三角
- iOS开发技巧之:UIImage的剪切,尺寸缩小、压缩、添加水印
- org.springframework.transaction.CannotCreateTransactionException:
- 蓝桥杯 ADV-178 算法提高 简单加法
- 打印斐波那契(Fibonacci)数列
- java基础_day0008_循环_for_无限循环
- 打印升序降序字母金字塔
- 162. Find Peak Element
- 概率无向图模型
- 冒泡排序