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

这篇文章主要给大家介绍了关于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;

总结

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

以上就是iOS中实现imageView任意角度旋转的方法的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 移动