UIImage图片处理,旋转、截取、平铺、缩放等操作
来源:互联网 发布:淘宝没有购物车营销 编辑:程序博客网 时间:2024/06/15 13:35
有时候我们需要处理图片,比如改变大小,旋转,截取等等,所以今天说一说图片处理相关的一些操作。
本文所说的方法都是写在UIImage的Category中,这样使用起来也方便;由于代码太多,这里就不贴具体实现代码了,大家可以去我的Github查看demo(https://github.com/lisongrc/UIImage-Categories),效果如下:
颜色相关
1.根据颜色生成纯色图片
就是根据制定的颜色生成一张纯色的图片
+(UIImage *)imageWithColor:(UIColor *)color;
使用方法,比如设置UIImageView的图片为红色纯图片:
self.imageView.image = [UIImageimageWithColor:[UIColorredColor]];
2.取图片上某一像素的颜色
有时候我们需要获取图片上的某一点的颜色,比如画板应用选择画笔颜色的时候,其实是在一张有所有颜色的图片上点击选择实现的。
需要注意的是这里要传的参数point是相对于图片上的点。
-(UIColor *)colorAtPixel:(CGPoint)point;
使用方法,比如我们在图片上加个tap手势,然后在响应方法里面这样写就可以了:
-(void)handleTap:(UITapGestureRecognizer *)tap
{
CGPointpoint = [taplocationInView:tap.view];
UIImage *image = self.imageView.image;
CGPointpointInImage = CGPointMake(point.x *image.size.width / self.imageView.frame.size.width,point.y *image.size.height / self.imageView.frame.size.height);
self.view.backgroundColor = [imagecolorAtPixel:pointInImage];
}
3.获得灰度图
获取一张彩色图片的黑白图片
-(UIImage *)convertToGrayImage;
使用方法:
self.imageView.image = [imageconvertToGrayImage];
旋转相关
1.纠正图片的方向
当我们需要读取相册的图片,发现相册里面的方向和展示出来的图片的方向不一样,这时候就要矫正方向了。
-(UIImage *)fixOrientation;
使用:
self.imageView.image = [imagefixOrientation];
2.按给定的方向旋转图片
在做图片处理工具的时候,我们可能需要旋转图片。
这个方法的参数是系统枚举UIImageOrientation。
typedefNS_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 = [imagerotate: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*)firstImagewithImage:(UIImage*)secondImage;
Gif相关
将一个Gif直接设置为UIImageView的image就可以显示动态图了。
/** 用一个Gif生成UIImage,传入一个GIFData */
+(UIImage *)animatedImageWithAnimatedGIFData:(NSData *)theData;
/** 用一个Gif生成UIImage,传入一个GIF路径 */
+(UIImage *)animatedImageWithAnimatedGIFURL:(NSURL *)theURL;
使用:
NSString *path = [[NSBundlemainBundle]pathForResource:@"gif"ofType:@"gif"];
self.imageView.image = [UIImageanimatedImageWithAnimatedGIFURL:[NSURLfileURLWithPath:path]];
//或者
self.imageView.image = [UIImageanimatedImageWithAnimatedGIFData:[NSDatadataWithContentsOfFile:path]];
希望能帮到大家,持续更新中。
- UIImage图片处理,旋转、截取、平铺、缩放等操作
- UIImage图片处理,旋转、截取、平铺、缩放等操作
- UIImage图片处理,旋转、截取、平铺、缩放等操作
- UIImage图片处理,旋转、截取、平铺、缩放等操作
- UIImage图片处理,旋转、截取、平铺、缩放等操作
- UIImage 图像截取,等比例缩放
- iOS 图片相关操作,如:翻转、缩放、截取等
- 关于uiimage的比例缩放、及图片截取
- php图片处理代码分享,包括缩放、剪裁、缩放、翻转、旋转、透明、锐化等
- 图片操作旋转等
- Xamarion.IOS UIImage图片的裁剪缩放等
- android 图片处理(截取,缩放)
- android 图片处理(截取,缩放)
- android 图片处理(截取,缩放)
- android 图片处理(截取,缩放)
- OpenCV图像操作,缩放,旋转等
- uiimage 图片缩放
- UIImage图片缩放功能
- xauth: (stdin):1: bad display name
- ROS探索总结(二)——ROS总体框架
- Standard C 语言标准函数库速查 (Cheat Sheet)
- 机器人动力学建模实例:二连杆机械臂
- 搭建svn服务器
- UIImage图片处理,旋转、截取、平铺、缩放等操作
- Ubuntu16.04配置小结
- Android MVP
- nodejs 模块的流程
- Struts中<s:debug/>标签中ValueStack与ContextStack的区别
- 新建JSP页面时报错The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- memcache与memcached的区别
- 数组中
- 微信小程序一(开始构建)