虚位以待(AD)
虚位以待(AD)
首页 > 软件编程 > IOS编程/Objective-C > iOS中实现imageView任意角度旋转的方法

iOS中实现imageView任意角度旋转的方法
类别:IOS编程/Objective-C   作者:码皇   来源:互联网   点击:

这篇文章主要给大家介绍了关于iOS中实现imageView任意角度旋转的方法,文中通过示例代码介绍的非常详细,对大家学习或者工作具有一定的参考学习价值,需要的朋友下面随着小编来一起学习学习吧。

前言

在实际的开发中我们可能会遇到这种情况: 需要对图片进行一定角度的旋转。对于这种需要,我们可能会用UIView的transform进行旋转,但是这样做其实只是对承载imageView的view进行了一定角度的旋转,而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 * 4size_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转换成imageCGImageRef 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;

总结

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

相关热词搜索: ios imageview 旋转 ios imageview旋转180