栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > iOS

iOS中实现imageView任意角度旋转的方法

iOS 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

iOS中实现imageView任意角度旋转的方法

前言

在实际的开发中我们可能会遇到这种情况: 需要对图片进行一定角度的旋转。对于这种需要,我们可能会用UIView的transform进行旋转,但是这样做其实只是对承载imageView的view进行了一定角度的旋转,而imageView并没有旋转。所有这样的做法并不好。

如果需要实现对imageView实现一定角度的旋转,具体步骤是:

      1.将image转成context。

      2.对context进行一定角度的旋转。

      3.将旋转后的context 转化成image。

经过这三个步骤,我们就能够实现将图片真正的做到旋转。

 好了,直接上代码:

#import"UIImage+RotateImageTool.h"
#import
#import
@implementationUIImage (RotateImageTool)
-(UIImage*)rotateImageWithDegree:(CGFloat)degree{
//将image转化成context
//获取图片像素的宽和高
size_t width =self.size.width*self.scale;
size_t height =self.size.height*self.scale;
//颜色通道为8因为0-255经过了8个颜色通道的变化
//每一行图片的字节数因为我们采用的是ARGB/RGBA所以字节数为width * 4
size_t bytesPerRow =width *4;
//图片的透明度通道
CGImageAlphaInfo info =kCGImageAlphaPremultipliedFirst;
//配置context的参数:
CGContextRef context =CGBitmapContextCreate(nil, width, height,8, bytesPerRow,CGColorSpaceCreateDeviceRGB(),kCGBitmapByteOrderDefault|info);
if(!context) {
return nil;
}
//将图片渲染到图形上下文中
CGContextDrawImage(context,CGRectMake(0,0, width, height),self.CGImage);
uint8_t* data = (uint8_t*)CGBitmapContextGetData(context);
//旋转欠的数据
vImage_Buffer src = { data,height,width,bytesPerRow};
//旋转后的数据
vImage_Buffer dest= { data,height,width,bytesPerRow};
//背景颜色
Pixel_8888 backColor = {0,0,0,0};
//填充颜色
vImage_Flags flags = kvImageBackgroundColorFill;
//旋转context 
vImageRotate_ARGB8888(&src, &dest,nil, degree *M_PI/180.f, backColor, flags);
//将conetxt转换成image
CGImageRef imageRef =CGBitmapContextCreateImage(context);
UIImage* rotateImage =[UIImageimageWithCGImage:imageRefscale:self.scaleorientation:self.imageOrientation];
returnrotateImage;
}

代码中有详细的注释,在这里我就不过多的解释了。感兴趣的可以到github上面下载哦。

下载地址:github.com/15221532825/ImageTool  (本地下载)

附:iOS ImageView的Image自适应缩放显示全套处理方法

// retina屏幕图片显示问题
[_detailImageView setContentScaleFactor:[[UIScreen mainScreen] scale]];
// 不规则图片显示
_detailImageView.contentMode = UIViewContentModeScaleAspectFill;
_detailImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
// 图片大于或小于显示区域
_detailImageView.clipsToBounds = YES;

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对考高分网的支持。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/166361.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号