美文网首页
[iOS] 图片旋转

[iOS] 图片旋转

作者: manajay | 来源:发表于2018-04-26 14:03 被阅读31次
    /**
     旋转图片
     @param radians 旋转弧度
     */
    - (UIImage *)lj_imageRotatedByRadians:(CGFloat)radians {
        return [self lj_imageRotatedByDegrees:RadiansToDegrees(radians)];
    }
    
    /**
     旋转图片
    
     @param degrees 旋转角度
     */
    - (UIImage *)lj_imageRotatedByDegrees:(CGFloat)degrees {
        
        // calculate the size of the rotated view's containing box for our drawing space
        UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width, self.size.height)];
        CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
        rotatedViewBox.transform = t;
        CGSize rotatedSize = rotatedViewBox.frame.size;
        rotatedViewBox = nil;
        
        // Create the bitmap context
        UIGraphicsBeginImageContextWithOptions(rotatedSize, NO, 0.0);
        CGContextRef bitmap = UIGraphicsGetCurrentContext();
        
        // Move the origin to the middle of the image so we will rotate and scale around the center.
        CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
        
        //   // Rotate the image context
        CGContextRotateCTM(bitmap, DegreesToRadians(degrees));
        
        // Now, draw the rotated/scaled image into the context
        CGContextScaleCTM(bitmap, 1.0, -1.0);
        CGContextDrawImage(bitmap, CGRectMake(-self.size.width / 2, -self.size.height / 2, self.size.width, self.size.height), [self CGImage]);
        
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage;
        
    }
    
    

    相关文章

      网友评论

          本文标题:[iOS] 图片旋转

          本文链接:https://www.haomeiwen.com/subject/pngplftx.html