来源:iOS_小松哥

链接:http://www.jianshu.com/p/9ab1205f5166

有时候我们需要处理图片,比如改变大小,旋转,截取等等,所以今天说一说图片处理相关的一些操作。

本文所说的方法都是写在UIImage的Category中,这样使用起来也方便;由于代码太多,这里就不贴具体实现代码了,大家可以去我的Github查看demo(https://github.com/lisongrc/UIImage-Categories),效果如下:

颜色相关

1.根据颜色生成纯色图片

就是根据制定的颜色生成一张纯色的图片

+ (UIImage *)imageWithColor:(UIColor *)color;

使用方法,比如设置UIImageView的图片为红色纯图片:

self.imageView.image = [UIImage imageWithColor:[UIColor redColor]];

2.取图片上某一像素的颜色

有时候我们需要获取图片上的某一点的颜色,比如画板应用选择画笔颜色的时候,其实是在一张有所有颜色的图片上点击选择实现的。

需要注意的是这里要传的参数point是相对于图片上的点。

- (UIColor *)colorAtPixel:(CGPoint)point;

使用方法,比如我们在图片上加个tap手势,然后在响应方法里面这样写就可以了:

- (void)handleTap:(UITapGestureRecognizer *)tap

{

CGPoint point = [tap locationInView:tap.view];

UIImage *image = self.imageView.image;

CGPoint pointInImage = CGPointMake(point.x * image.size.width / self.imageView.frame.size.width, point.y * image.size.height / self.imageView.frame.size.height);

self.view.backgroundColor = [image colorAtPixel:pointInImage];

}

3.获得灰度图

获取一张彩色图片的黑白图片

- (UIImage *)convertToGrayImage;

使用方法:

self.imageView.image = [image convertToGrayImage];

旋转相关

1.纠正图片的方向

当我们需要读取相册的图片,发现相册里面的方向和展示出来的图片的方向不一样,这时候就要矫正方向了。

- (UIImage *)fixOrientation;

使用:

self.imageView.image = [image fixOrientation];

2.按给定的方向旋转图片

在做图片处理工具的时候,我们可能需要旋转图片。

这个方法的参数是系统枚举UIImageOrientation。

typedef NS_ENUM(NSInteger, UIImageOrientation) {

UIImageOrientationUp,            // default orientation

UIImageOrientationDown,          // 180 deg rotation

UIImageOrientationLeft,          // 90 deg CCW

UIImageOrientationRight,         // 90 deg CW

UIImageOrientationUpMirrored,    // as above but image mirrored along other axis. horizontal flip

UIImageOrientationDownMirrored,  // horizontal flip

UIImageOrientationLeftMirrored,  // vertical flip

UIImageOrientationRightMirrored, // vertical flip

};

- (UIImage*)rotate:(UIImageOrientation)orient;

使用,比如顺时针旋转180度:

self.imageView.image = [image rotate:UIImageOrientationDown];

3.垂直翻转

其实就是上面的方法传UIImageOrientationDownMirrored参数。

- (UIImage *)flipVertical;

4.水平翻转

其实就是传UIImageOrientationUpMirrored参数。

- (UIImage *)flipHorizontal;

5.将图片旋转degrees角度

传入一个自定义的角度。

- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees;

6.将图片旋转radians弧度

- (UIImage *)imageRotatedByRadians:(CGFloat)radians;

生成图相关

1.截取image对象rect区域内的图像

- (UIImage *)subImageWithRect:(CGRect)rect;

2.压缩图片至指定尺寸

- (UIImage *)rescaleImageToSize:(CGSize)size;

3.压缩图片至指定像素

- (UIImage *)rescaleImageToPX:(CGFloat )toPX;

4.生成一个size大小的平铺图片

- (UIImage *)getTiledImageWithSize:(CGSize)size;

5..UIView转化为UIImage

+ (UIImage *)imageFromView:(UIView *)view;

6.将两个图片生成一张图片

firstImage在下面,secondImage在上面

+ (UIImage*)mergeImage:(UIImage*)firstImage withImage:(UIImage*)secondImage;

Gif相关

将一个Gif直接设置为UIImageView的image就可以显示动态图了。

/** 用一个Gif生成UIImage,传入一个GIFData */

+ (UIImage *)animatedImageWithAnimatedGIFData:(NSData *)theData;

/** 用一个Gif生成UIImage,传入一个GIF路径 */

+ (UIImage *)animatedImageWithAnimatedGIFURL:(NSURL *)theURL;

使用:

NSString *path = [[NSBundle mainBundle] pathForResource:@"gif" ofType:@"gif"];

self.imageView.image = [UIImage animatedImageWithAnimatedGIFURL:[NSURL fileURLWithPath:path]];

//或者

self.imageView.image = [UIImage animatedImageWithAnimatedGIFData:[NSData dataWithContentsOfFile:path]];

希望能帮到大家,持续更新中。

UIImage图片处理,旋转、截取、平铺、缩放等操作的更多相关文章

  1. 在xml中使用图片资源时,设置重复图片而不是拉伸平铺

    直接把图片放入xml中时,默认会拉伸图片填充.而用下面的方法则可以实现重复图片 <?xml version="1.0" encoding="utf-8"? ...

  2. LODOP中设置设置图片平铺水印,超文本透明

    之前的博文:LODOP中平铺图片 文本项Repeat. 该博文中是平铺的图片,上面是文本.如果是图片add_print_image和add_print_text纯文本,这两个打印项设计的,可以直接通过 ...

  3. ios 缩放图片(平铺)

    //缩放图片(平铺) - (UIImage *)resizeImage:(NSString *)imgName { UIImage *bgImage =  [UIImage imageNamed:im ...

  4. android实现图片平铺效果&WebView多点触控实现缩放

    1.图片平铺效果实现非常简单,只要在xml中添加一个 android:tileMode的属性就可以了.首先在drawable文件夹中添加自己的my.xml文件.代码: Java代码 <?xml ...

  5. [ATL/WTL]_[0基础]_[CBitmap复制图片-截取图片-平铺图片]

    场景: 1.当你须要截取图片部分区域作为某个控件的背景. 2.须要平铺图片到一个大区域让他自己主动放大时. 3.或者须要合并图片时. 代码: CDC sdc; CDC ddc; sdc.CreateC ...

  6. iOS UIButton 设置图片平铺

    UIImage *image2 = [UIImage imageNamed:imgName]; CGFloat top = ; // 顶端盖高度 CGFloat bottom = ; // 底端盖高度 ...

  7. Duilib技巧:背景图片平铺

    贴图的描述 方式有两种    // 1.aaa.jpg    // 2.file='aaa.jpg' res='' restype='0' dest='0,0,0,0' source='0,0,0,0 ...

  8. 69、Android 布局中轻松实现图片的全屏、居中、平铺

    public void paint() { if (item.laying_mode != 1)//平铺或者充满 { new AsyncTask<Void, Void, Void>() { ...

  9. QPainter绘制图片填充方式(正常大小、剪切大小、自适应大小、平铺)

    Qt中QPainter提供了绘制图像的API,极大地方便了我们对图像的绘制. Qt中提供了QPixmap, QBitmap,QBitMapQImage,QPicture等图像绘图设备,它们的类关系如下 ...

随机推荐

  1. Spark RDD概念学习系列之RDD的缓存(八)

      RDD的缓存 RDD的缓存和RDD的checkpoint的区别 缓存是在计算结束后,直接将计算结果通过用户定义的存储级别(存储级别定义了缓存存储的介质,现在支持内存.本地文件系统和Tachyon) ...

  2. ntpdate server时出错原因及解决

    错误1.Server dropped: Strata too high 在ntp客户端运行ntpdate serverIP,出现no server suitable for synchronizati ...

  3. UIView UITableView 背景图片添加

    这几天,经常用到为某个视图设置背景图片,而API中UIView没有设置背景图片的方法,搜集归纳如下: 第一种方法: 利用的UIView的设置背景颜色方法,用图片做图案颜色,然后传给背景颜色. UICo ...

  4. 使用aspose.word两句代码将word转换为pdf

    //Load Document Document document = new Document(@"C:\Users\Administrator\Desktop\人事---新员工转正总结( ...

  5. 红米手机拍照效果测评(对比小米2A)

    小米相关的产品一向都很很受用户的欢迎,一个就是实惠,另一个就是配置还不错.近期小米推出的红米手机可谓是先声夺人,关注度异常火爆.今天刚抢的红米快递寄到了,来测试下红米手机的拍照表现,800万像素怎么样 ...

  6. WebSphere 集群环境下配置 Quartz集群

    转载:http://hyamine.iteye.com/blog/397708 1. websphere工作管理器引用 WEB-INF/ibm-web-bnd.xmi <?xml version ...

  7. 粗解Xcode 5新特性: Asset Catalogs

    原文:http://schlu.org/2013/10/01/Xcode-Asset-Catalogs.html 基础知识 在今年的WWDC大会上苹果介绍了Asset Catalogs.Asset C ...

  8. json操作json类型转换

    前提是需要加Jar包: gson-2.2.2.jar package utils; import java.io.BufferedReader;import java.io.FileInputStre ...

  9. 第四章TPLINK 703n 重要恢复方法,非TTL串口连接

    途中有一次为了试图能够在703N上挂载普通usb(可用空间只有2M多点),卸载了不少系统软件,甚至把UCI给卸载了,导致系统起来后没有SSH服务,只有DNS服务,几乎变砖.百般无奈下,终于找到有高人提 ...

  10. js 数组去重 的5种方法

    一万数组,4个重复项,先贴上成绩. 1.3毫秒 2.115毫秒 3.71毫秒 4.6毫秒 1.哈希表 2.JQuery (最快的方法是用JQuery 这句话是截图带的... 实际上Jq是最慢的) 3. ...