[转]常用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 ...
随机推荐
- Virt-install用法:
#一般选项:指定虚拟机的名称.内存大小.VCPU个数及特性等 -n NAME, --name=NAME:虚拟机名称,需全局惟一: -r MEMORY, --ram=MEMORY:虚拟机内 ...
- 【线段树 扫描线 二维数点】loj#6276. 果树
路径计数转成二维数点很妙啊 题目描述 NiroBC 姐姐是个活泼的少女,她十分喜欢爬树,而她家门口正好有一棵果树,正好满足了她爬树的需求. 这颗果树有 $N$ 个节点,标号 $1 \ldots N$ ...
- [LUOGU] NOIP提高组模拟赛Day1
题外话:以Ingress为题材出的比赛好评,绿军好评 T1 考虑枚举第\(i\)个人作为左边必选的一个人,那左边剩余\(i-1\)个人,选法就是\(2^{i-1}\),也就是可以任意选或不选,右侧剩余 ...
- char与varchar的区别与联系
char是字节类型,varcahr是字符类型 1.char(20) 存放的是字节,utf-8中文字符占三个字节,GB18030兼容GBK兼容GB2312中文字符占两个字节,ISO8859-1是拉丁字符 ...
- python基础学习笔记——类空间问题以及类之间的关系
一. 类的空间问题 1.1 何处可以添加对象属性 class A: def __init__(self,name): self.name = name def func(self,sex): self ...
- java事物
[引用:http://www.cnblogs.com/kristain/articles/2038397.html] 一.什么是事务 事务是访问数据库的一个操作序列,数据库应用系统通过事务集来完成对数 ...
- Scala学习-02-方法
算数和操作符重载 所有的操作符都是方法. a + b 是一种缩写形式 : a .+ b “+”是方法名(操作符重载) ++和—— Scala中并没有“++”和“——”.需要使用“+=”和“-=” ...
- Selenium WebDriver- 指定页面加载时间
#encoding=utf-8 import unittest import time from selenium import webdriver from selenium.webdriver i ...
- cinema 4d 包括宝典 --- 改线 循环边 建模布线原则
cinema 4d 一.视图控制与物体控制 1.摇移 alt+鼠标左键 转圈看物体 改变角度 2.平移 alt +鼠标中键 不改变角度 移动 3.推拉 alt+鼠标右键 ...
- Uiautomator ---(1) 封装代码
http://www.cnblogs.com/by-dream/p/4996000.html 上面是别人的写法 我自己的写法: package qq.test; import android.con ...