UIImage 相关操作
修改UIImage大小
修改UISlider的最大值和最小值图片的时候,发现需要修改图片的大小,否则会导致UISlider变形。目前苹果还不支持直接修改UIImage类的大小,只能修改UIImageView的大小。
所以只能通过写方法的方式来实现修改图片。如下:
//UIImage.h #import <Foundation/Foundation.h> @interface UIImage (Scale) -(UIImage *)TransformtoSize:(CGSize)Newsize; @end //UIImage.m #import "UIImage.h" @implementation UIImage (Scale) -(UIImage *)TransformtoSize:(CGSize)Newsize { // 创建一个bitmap的context UIGraphicsBeginImageContext(Newsize); // 绘制改变大小的图片 [self drawInRect:CGRectMake(, , Newsize.width, Newsize.height)]; // 从当前context中创建一个改变大小后的图片 UIImage *TransformedImg=UIGraphicsGetImageFromCurrentImageContext(); // 使当前的context出堆栈 UIGraphicsEndImageContext(); // 返回新的改变大小后的图片 return TransformedImg; } @end 最后在其他地方导入头文件,即可以使用此方法了。 UIImage *MaxImg=[UIImage imageNamed:@"sunny.png"]; UIImage *MaxImg_Fin=[MaxImg TransformtoSize:CGSizeMake(, )];
调整UIImage尺寸适应UIImageView
+ (UIImage*)imageWithImage:(UIImage*)image
scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(,,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); return newImage;
}
指定新尺寸(768,1024)
然后传递初始图片,但是要保证高宽比和原始的比例一样。
--------------------------------------------
http://my.oschina.net/rareliu/blog/24322
UIImage 图片处理:截图,缩放,设定大小,存储
发表于3年前(2011-06-21 15:35)
图片的处理大概就分 截图(capture), 缩放(scale),设定大小(resize), 存储(save)
这几样比较好处理, 另外还有滤镜,擦试等, 以后再说
在这个Demo code裡, 我写了几个方法
1.等比率缩放
- (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize
{ UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize);
[image drawInRect:CGRectMake(, , image.size.width * scaleSize, image.size.height * scaleSize)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); return scaledImage; }
2.自定长宽
- (UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)reSize
{
UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));
[image drawInRect:CGRectMake(, , reSize.width, reSize.height)];
UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); return reSizeImage; }
3.处理某个特定View
只要是继承UIView的object 都可以处理
必须先import QuzrtzCore.framework
-(UIImage*)captureView:(UIView *)theView
{
CGRect rect = theView.frame;
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[theView.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); return img; }
4.储存图片
储存图片这里分成储存到app的文件里, 储存到手机的图片库里
1) 储存到app的文件里
NSString *path = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"image.png"];
[UIImagePNGRepresentation(image) writeToFile:pathatomically:YES];
這樣就把你要處理的圖片, 以image.png這個檔名存到app home底下的Documents目錄裡
2)储存到手机的图片库里
CGImageRef screen = UIGetScreenImage();
UIImage* image = [UIImage imageWithCGImage:screen];
CGImageRelease(screen);
UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
UIGetScreenImage()原本是private(私有)api, 用來截取整個畫麵不過SDK 4.0後apple就開放了
另外儲存到手機的圖片庫裡, 必須在實機使用, 模擬器無法使用
以下代碼用到了Quartz Framework和Core Graphics Framework. 在workspace的framework目錄裏添加這兩個framework.在UIKit裏,圖像類UIImage和CGImageRef的畫圖操作都是通過Graphics Context來完成。Graphics Context封裝了變換的參數,使得在不同的坐標係裏操作圖像非常方便。缺點就是,獲取圖像的數據不是那麼方便。下麵會給出獲取數據區的代碼。
從UIView中獲取圖像相當於窗口截屏。ios提供全局的全屏截屏函數UIGetScreenView(). 如果需要特定區域的圖像,可以crop一下。
CGImageRef screen = UIGetScreenImage();
UIImage* image = [UIImage imageWithCGImage:screen];
對於特定UIView的截屏,可以把當前View的layer,輸出到一個ImageContext中,然後利用這個ImageContext得到UIImage
-(UIImage*)captureView: (UIView *)theView
{
CGRect rect = theView.frame;
UIGraphicsBeginImageContext(rect.size);
CGContextRef context =UIGraphicsGetCurrentContext();
[theView.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); return img;
}
如果需要裁剪製定區域,可以path & clip,以下例子是建一個200x200的圖像上下文,再截取出左上角
UIGraphicsBeginImageContext(CGMakeSize(,));
CGContextRefcontext=UIGraphicsGetCurrentContext();
UIGraphicsPushContext(context);
// ...把图写到context中,省略[indent]CGContextBeginPath();
CGContextAddRect(CGMakeRect(,,,));
CGContextClosePath();[/indent]CGContextDrawPath();
CGContextFlush(); // 强制执行上面定义的操作
UIImage* image = UIGraphicGetImageFromCurrentImageContext();
UIGraphicsPopContext();
存储图像分为存储到home目录文件和图片库文件。存储到目录文件是这样
NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"image.png"]; [UIImagePNGRepresentation(image) writeToFile:path atomically:YES];
若要存储到图片库里面
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
UImage封装了CGImage, 互相转换很容易
UIImage* imUI=nil;
CGImageRef imCG=nil;
imUI = [UIImage initWithCGImage:imCG];
imCG = imUI.CGImage;
從CGImage上獲取圖像數據區,在apple dev上有QA, 不過好像還不支持ios
下麵給出一個在ios上反色的例子
-(id)invertContrast:(UIImage*)img
{
CGImageRef inImage = img.CGImage;
CGContextRef ctx;
CFDataRef m_DataRef;
m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage)); int width = CGImageGetWidth( inImage );
int height = CGImageGetHeight( inImage ); int bpc = CGImageGetBitsPerComponent(inImage);
int bpp = CGImageGetBitsPerPixel(inImage);
int bpl = CGImageGetBytesPerRow(inImage); UInt8 * m_PixelBuf = (UInt8 *) CFDataGetBytePtr(m_DataRef);
int length = CFDataGetLength(m_DataRef); NSLog(@"len %d", length);
NSLog(@"width=%d, height=%d", width, height);
NSLog(@"1=%d, 2=%d, 3=%d", bpc, bpp,bpl); for (int index = ; index < length; index += )
{
m_PixelBuf[index + ] = - m_PixelBuf[index + ];// b
m_PixelBuf[index + ] = - m_PixelBuf[index + ];// g
m_PixelBuf[index + ] = - m_PixelBuf[index + ];// r
} ctx = CGBitmapContextCreate(m_PixelBuf, width, height, bpb, bpl, CGImageGetColorSpace( inImage ), kCGImageAlphaPremultipliedFirst );
CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
UIImage* rawImage = [UIImage imageWithCGImage:imageRef];
CGContextRelease(ctx);
return rawImage;
}
得到圖像數據區後就可以很方便的實現圖像處理的算法。下麵給顯示圖像數據區的方法,也就是unsigned char*轉為graphics context或者UIImage或和CGImageRef
CGContextRef ctx = CGBitmapContextCreate(pixelBuf,width,height, bitsPerComponent,bypesPerLine, colorSpace,kCGImageAlphaPremultipliedLast );
CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
UIImage* image = [UIImage imageWithCGImage:imageRef];
NSString* path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"ss.png"];
[UIImagePNGRepresentation(self.image) writeToFile:path atomically:YES];
CGContextRelease(ctx);
UIImage 相关操作的更多相关文章
- 从零自学Hadoop(20):HBase数据模型相关操作上
阅读目录 序 介绍 命名空间 表 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,SourceLink 序 ...
- 从零自学Hadoop(21):HBase数据模型相关操作下
阅读目录 序 变量 数据模型操作 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,SourceLink 序 ...
- 理解CSV文件以及ABAP中的相关操作
在很多ABAP开发中,我们使用CSV文件,有时候,关于CSV文件本身的一些问题使人迷惑.它仅仅是一种被逗号分割的文本文档吗? 让我们先来看看接下来可能要处理的几个相关组件的词汇的语义. Separat ...
- Liunx下的有关于tomcat的相关操作 && Liunx 常用指令
先记录以下liunx下的有关于tomcat的相关操作 查看tomcat进程: ps-ef|grep java (回车) 停止tomcat进程: kill -9 PID (进程号如77447) (回车) ...
- pip的相关操作
>Python中的pip是什么?能够做些什么? pip是Python中的一个进行包管理的东西,能够下载包.安装包.卸载包......一些列操作 >怎么查看pip的相关信息 在控制台输入: ...
- python操作mysql数据库的相关操作实例
python操作mysql数据库的相关操作实例 # -*- coding: utf-8 -*- #python operate mysql database import MySQLdb #数据库名称 ...
- php对二维数组进行相关操作(排序、转换、去空白等)
php对二维数组进行相关操作(排序.转换.去空白等) 投稿:lijiao 字体:[增加 减小] 类型:转载 时间:2015-11-04 这篇文章主要介绍了php对二维数组进行相关操作,包括php对 ...
- SQL语言和DML相关操作以及相应的运算符
SQL 1.结构化查询语言 2.特点 a.第四代编程语言,更接近自然语言 b必须有数据库系统解释执行 c.对象名,关键字不区分大小写 d.字符串必须要用单引号引起来,不能用双引号 e.每条语句最后用分 ...
- 如何给ActiveX控件添加“事件”“属性”“标准事件”“自定义事件”等一些相关操作
上一篇小编带大家熟悉了一下ActiveX的建立以及相关的概念,(http://blog.csdn.net/u014028070/article/details/38424611) 本文介绍下如何给控件 ...
随机推荐
- 注意:C++中double的表示是有误差的
注意:C++中double的表示是有误差的,直接通过下面的例子看一下 #include<iostream> using namespace std; int main() { double ...
- 双方都在线,qq总是离线发文件
这是qq支持多地登录后出现的问题. 原因:1.当您传文件给对方,对方是多终端登录(或者开通移动在线功能)的情况下,为了保证对方一定能收到该文件,我们会智能的为用户切换到离线文件,对方会相应在所在的终端 ...
- 关于BigDecimal的四舍五入和截断 (2007-08-10 15:06:26)
关于四舍五入:ROUND_HALF_UP: 遇到.5的情况时往上近似,例: 1.5 ->;2ROUND_HALF_DOWN : 遇到.5的情况时往下近似,例: 1.5 ->;1 BigDe ...
- JavaScript constructor 属性
定义和用法 constructor 属性返回对创建此对象的数组函数的引用. 语法 object.constructor 实例 例子 1 在本例中,我们将展示如何使用 constructor 属性: & ...
- 1160. Network(最小生成树)
1160 算是模版了 没什么限制 结束输出就行了 #include <iostream> #include<cstdio> #include<cstring> #i ...
- Delegate 委托复习(-) 委托的基本概念
1. 声明一个delegate对象,它应当与你想要传递的方法具有相同的参数和返回值类型. 声明一个代理的例子: public delegate int MyDelegate(stri ...
- UVA 11478 Halum(用bellman-ford解差分约束)
对于一个有向带权图,进行一种操作(v,d),对以点v为终点的边的权值-d,对以点v为起点的边的权值+d.现在给出一个有向带权图,为能否经过一系列的(v,d)操作使图上的每一条边的权值为正,若能,求最小 ...
- LeetCode: Sqrt
Title: Implement int sqrt(int x). Compute and return the square root of x. 思路:这个平方根肯定是在[1,x]之间,所以在这个 ...
- 数据库语言(二):SQL语法实例整理
连接表达式: select * from student join takes on student.ID = takes.ID; 通过on后面的谓词作为连接条件,相同的属性可以出现两次,也就是等价于 ...
- linux lnmp编译安装
关闭SELINUX vi /etc/selinux/config #SELINUX=enforcing #注释掉 #SELINUXTYPE=targeted #注释掉 SELINUX=disabled ...