美文网首页
ios 获取旋转图片后的图片

ios 获取旋转图片后的图片

作者: songjk | 来源:发表于2020-08-12 18:01 被阅读0次

旋转UIImage,并拿到旋转后的图片数据

        - (UIImage *)imageRotatedByRadians:(CGFloat)radians
    {
        // calculate the size of the rotated view's containing box for our drawing space
        UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.targetImage.size.width, self.targetImage.size.height)];
        CGAffineTransform t = CGAffineTransformMakeRotation(radians);
        rotatedViewBox.transform = t;
        CGSize rotatedSize = rotatedViewBox.frame.size;
        
        // Create the bitmap context
        UIGraphicsBeginImageContext(rotatedSize);
        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, radians);
        
        // Now, draw the rotated/scaled image into the context
        CGContextScaleCTM(bitmap, 1.0, -1.0);
        CGContextDrawImage(bitmap, CGRectMake(-self.targetImage.size.width / 2, -self.targetImage.size.height / 2, self.targetImage.size.width, self.targetImage.size.height), [self.targetImage CGImage]);
        
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        return newImage;
    }

相关文章

网友评论

      本文标题:ios 获取旋转图片后的图片

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