iOS UIImage 图像旋转

来源:互联网 发布:php 数值比较 编辑:程序博客网 时间:2024/04/29 01:36
/* iOS UIImage 图像旋转 vImg:待旋转的图 vAngle:旋转角度 vIsExpand:是否扩展,如果不扩展,那么图像大小不变,但被截掉一部分 */- (UIImage*)rotateImageWithAngle:(UIImage*)vImg Angle:(CGFloat)vAngle IsExpand:(BOOL)vIsExpand{    CGSize imgSize = CGSizeMake(vImg.size.width * vImg.scale, vImg.size.height * vImg.scale);        CGSize outputSize = imgSize;    if (vIsExpand) {        CGRect rect = CGRectMake(0, 0, imgSize.width, imgSize.height);        //旋转        rect = CGRectApplyAffineTransform(rect, CGAffineTransformMakeRotation(vAngle*M_PI/180.0));            //NSLog(@"rotateImageWithAngle, size0:%f, size1:%f", imgSize.width, rect.size.width);        outputSize = CGSizeMake(CGRectGetWidth(rect), CGRectGetHeight(rect));    }        UIGraphicsBeginImageContext(outputSize);    CGContextRef context = UIGraphicsGetCurrentContext();        CGContextTranslateCTM(context, outputSize.width / 2, outputSize.height / 2);    CGContextRotateCTM(context, vAngle*M_PI/180.0);    CGContextTranslateCTM(context, -imgSize.width / 2, -imgSize.height / 2);        [vImg drawInRect:CGRectMake(0, 0, imgSize.width, imgSize.height)];        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();        return image;}

0 0