遇到这个问题了,自然明白.....
- 起初,只是旋转了imageView,image并没有真正旋转,代码如下:
double Degree=45.0/180.0;
CGAffineTransform transform= CGAffineTransformMakeRotation(-M_PI* Degree);
bgimgView.transform = transform;//旋转bgimgView,没有旋转到bgimgView.image
- 拿到旋转后的image(致敬开源精神的大佬):
+ (UIImage *)ehs_imageWithRotation:(UIImage *)image rotationDegree:(CGFloat)degree
{
//将image转化成context
//获取图片像素的宽和高
size_t width = image.size.width * image.scale;
size_t height = image.size.height * image.scale;
//颜色通道为8 因为0-255 经过了8个颜色通道的变化
//每一行图片的字节数 因为我们采用的是ARGB/RGBA 所以字节数为 width * 4
size_t bytesPerRow =width * 4;
//图片的透明度通道
CGImageAlphaInfo info =kCGImageAlphaPremultipliedFirst;
CGContextRef context = CGBitmapContextCreate(nil, width, height, 8, bytesPerRow, CGColorSpaceCreateDeviceRGB(), kCGBitmapByteOrderDefault|info);
if (!context) {
return nil;
}
//将图片渲染到图形上下文中
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image.CGImage);
//旋转context
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;
vImageRotate_ARGB8888(&src, &dest, nil, degree * M_PI/180.f, backColor, flags);
//将conetxt转换成image
CGImageRef imageRef = CGBitmapContextCreateImage(context);
UIImage * rotateImage =[UIImage imageWithCGImage:imageRef scale:image.scale orientation:image.imageOrientation];
return rotateImage;
}