iOS中如何实现imageView任意角度旋转

发布时间:2021-08-04 13:47:43 作者:小新
来源:亿速云 阅读:184

这篇文章主要介绍iOS中如何实现imageView任意角度旋转,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

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

      1.将image转成context。

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

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

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

 好了,直接上代码:

#import"UIImage+RotateImageTool.h"
#import<QuartzCore/QuartzCore.h>
#import<Accelerate/Accelerate.h>
@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;
}

iOS中如何实现imageView任意角度旋转

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

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

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

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

以上是“iOS中如何实现imageView任意角度旋转”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. Android实现可旋转的ImageView
  2. CSS实现任意角度扇形的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ios imageview

上一篇:iOS应用程序生命周期的示例分析

下一篇:如何解决某些HTML字符打不出来的问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》