UIImageView: get transformed UIImage

后端 未结 3 803
被撕碎了的回忆
被撕碎了的回忆 2021-01-14 02:06

I\'m rotating UIImageView using this:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:UI         


        
3条回答
  •  既然无缘
    2021-01-14 02:49

    it works for me , and another way to solve

    - (UIImage *)imageRotatedByDegrees:(CGFloat)degrees
    {
        CGRect imgRect = CGRectMake(0, 0, self.size.width, self.size.height);
        CGAffineTransform transform = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
        CGRect rotatedRect = CGRectApplyAffineTransform(imgRect, transform);
        CGSize rotatedSize = rotatedRect.size;
    
        // Create the bitmap context
        UIGraphicsBeginImageContext(rotatedRect.size);
        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;
    }
    

提交回复
热议问题