[转]常用iOS图片处理方法
+ (UIImage *)getPicZoomImage:(UIImage *)image {
UIImage *img = image;
int h = img.size.height;
int w = img.size.width;
if(h <= PicAfterZoomWidth && w <= PicAfterZoomHeight)
{
image = img;
}
else
{
float b = (float)PicAfterZoomWidth/w < (float)PicAfterZoomHeight/h ? (float)PicAfterZoomWidth/w : (float)PicAfterZoomHeight/h;
CGSize itemSize = CGSizeMake(b*w, b*h);
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(0, 0, b*w, b*h);
[img drawInRect:imageRect];
img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return img;
}
=============== ( two )把图片 圆角 化==================================
static void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth,
float ovalHeight)
{
float fw, fh;
if (ovalWidth == 0 || ovalHeight == 0) {
CGContextAddRect(context, rect);
return;
}
CGContextSaveGState(context);
CGContextTranslateCTM(context, CGRectGetMinX(rect), CGRectGetMinY(rect));
CGContextScaleCTM(context, ovalWidth, ovalHeight);
fw = CGRectGetWidth(rect) / ovalWidth;
fh = CGRectGetHeight(rect) / ovalHeight;
CGContextMoveToPoint(context, fw, fh/2); // Start at lower right corner
CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1); // Top right corner
CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1); // Top left corner
CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1); // Lower left corner
CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); // Back to lower right
CGContextClosePath(context);
CGContextRestoreGState(context);
}
+ (id) createRoundedRectImage:(UIImage*)image size:(CGSize)size
{
// the size of CGContextRef
int w = size.width;
int h = size.height;
UIImage *img = image;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace,kCGImageAlphaPremultipliedFirst);
CGRect rect = CGRectMake(0, 0, w, h);
CGContextBeginPath(context);
addRoundedRectToPath(context, rect, 10, 10);
CGContextClosePath(context);
CGContextClip(context);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return [UIImage imageWithCGImage:imageMasked];
}
=============== (Three)给图片 添加阴影==================================
請先添加库 import QuartzCore.framework
然后要导入头文件 #import <QuartzCore/QuartzCore.h>
[[myView layer] setShadowOffset:CGSizeMake(5, 5)]; //设置阴影起点位置
[[myView layer] setShadowRadius:6]; //设置阴影扩散程度
[[myView layer] setShadowOpacity:1]; //设置阴影透明度
[[myView layer] setShadowColor:[UIColor blueColor].CGColor]; //设置阴影颜色
========== (Four) UIImage 图像 旋转==================================
- (UIImage *)imageRotatedByRadians:(CGFloat)radians
{
return [self imageRotatedByDegrees:radians * 180/M_PI];
}
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees
{
// calculate the size of the rotated view's containing box for our drawing space
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width,self.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(degrees * M_PI / 180);
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;
[rotatedViewBox release];
// Create the bitmap context
UIGraphicsBeginImageContext(rotatedSize);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
// Move the origin to the middle of the image so we will rotate and scale around the center.
CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
// // Rotate the image context
CGContextRotateCTM(bitmap, degrees * M_PI / 180);
// Now, draw the rotated/scaled image into the context
CGContextScaleCTM(bitmap, 1.0, -1.0);
CGContextDrawImage(bitmap, CGRectMake(-self.size.width / 2, -self.size.height / 2,self.size.width, self.size.height), [self CGImage]);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
图像旋转更多请看:http://www.catamount.com/blog/1015/uiimage-extensions-for-cutting-scaling-and-rotating-uiimages/
[转]常用iOS图片处理方法的更多相关文章
- iOS 图片压缩方法
iOS 图片压缩方法 两种图片压缩方法 两种压缩图片的方法:压缩图片质量(Quality),压缩图片尺寸(Size). 压缩图片质量 NSData *data = UIImageJPEGReprese ...
- iOS 图片裁剪方法
iOS 图片裁剪方法 通过 CGImage 或 CIImage 裁剪 UIImage有cgImage和ciImage属性,分别可以获得CGImage和CIImage对象.CGImage和CIImage ...
- iOS 图片旋转方法
iOS 图片旋转方法 通过 CGImage 或 CIImage 旋转特定角度 UIImage可通过CGImage或CIImage初始化,初始化方法分别为init(cgImage: CGImage, s ...
- C#常用的图片处理方法-图片剪切、图片压缩、多图合并代码
/// <summary> /// 图片转成圆角方法二 /// </summary> private Bitmap WayTwo(Bitmap bitmap) { //usin ...
- CSS常用背景图片定位方法
CSS背景图片定位其实对于每一位学习前端的同学来说,都已经非常熟悉了.网上铺天盖地的最常见的一种方案就是在父元素中relative,然后子元素absolute.这种方案当然好,不过带来的一个缺点就是会 ...
- ios 图片拉伸方法
前提:要注意图片的size和展示的图片view的size的大小. 假如图片高度50,展示图片view的高度30,拉伸会变成剪切. 如果图片尺寸不对,可以用mac自带的图片编辑器修改大小: 双击打开图 ...
- iOS 图片裁剪 + 旋转
iOS 图片裁剪 + 旋转 之前分别介绍了图片裁剪和图片旋转方法 <iOS 图片裁剪方法> 地址:http://www.cnblogs.com/silence-cnblogs/p/6490 ...
- MATLAB(4)——图片保存方法汇总及常用指令
作者:桂. 时间:2017-03-03 19:30:03 链接:http://www.cnblogs.com/xingshansi/p/6498318.html 前言 本文为MATLAB系列第四篇. ...
- iOS图片加载框架-SDWebImage解读
在iOS的图片加载框架中,SDWebImage可谓是占据大半壁江山.它支持从网络中下载且缓存图片,并设置图片到对应的UIImageView控件或者UIButton控件.在项目中使用SDWebImage ...
随机推荐
- 转 WebService两种发布协议--SOAP和REST的区别
转发文章 https://blog.csdn.net/zl834205311/article/details/62231545?ABstrategy=codes_snippets_optimize_v ...
- SQL Server ALwayson 正在解析
原因:把主库切换到辅助副本以后,集群全部出现正在解析的情况,数据库显示“恢复挂起” 过程:把服务器重启,原以为正在解析会恢复正常.结果失败. 解决方法:出现“正在解析”的情况跟故障转移群集有关,进故障 ...
- LeetCode(290) Word Pattern
题目 Given a pattern and a string str, find if str follows the same pattern. Here follow means a full ...
- JDK1.8 HashMap$TreeNode.balanceInsertion 红黑树平衡插入
红黑树介绍 1.节点是红色或黑色. 2.根节点是黑色. 3.每个叶子节点都是黑色的空节点(NIL节点). 4 每个红色节点的两个子节点都是黑色.(从每个叶子到根的所有路径上不能有两个连续的红色节点) ...
- Python中的魔法函数__repr__和__str__的实质性区别
str 和 repr 方法:是自定义类的字符串描述,这两种都是比较 Pythonic 的方式去控制对象转化为字符串的方式. 调用这两个方法,返回的都是字符串.但是这两个方法又有一些区别 ** 1 两种 ...
- LayoutInflater的用法
Instantiates a layout XML file into its corresponding View objects. It is never used directly. Inste ...
- 《小团团团队》第九次团队作业:Beta冲刺与验收准备
项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 实验十三 团队作业9:Beta冲刺与团队项目验收 团队名称 小团团团队 作业学习目标 (1)掌握软件黑盒测试技术:(2)学 ...
- python算法-排列组合
排列组合 一.递归 1.自己调用自己 2.找到一个退出的条件 二.全排列:针对给定的一组数据,给出包含所有数据的排列的组合 1:1 1,2:[[1,2],[2,1]] 1,2,3:[[1,2,3],[ ...
- Django创建
Pycharm里面Django模块安装及项目创建和启动: Pycharm里面Django模块安装(也可以指定安装源): 创建Django项目: 注意切换到合适的目录进行安装 diango-admin ...
- 编译安装solr
1, 获取安装包 wget http://download.oracle.com/otn-pub/java/jdk/8u112-b15/jdk-8u112-linux-x64.rpm wget htt ...