How to rotate an image 90 degrees on iOS?

后端 未结 14 1861
野的像风
野的像风 2020-11-29 00:55

What I want to do is take a snapshot from my camera , send it to a server and then the server sends me back the image on a viewController. If the image is in portrait mode t

14条回答
  •  时光取名叫无心
    2020-11-29 01:30

    - (UIImage *)rotateImage:(UIImage*)image byDegree:(CGFloat)degrees 
    {   
        UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,image.size.width, image.size.height)];
        CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
        rotatedViewBox.transform = t;
        CGSize rotatedSize = rotatedViewBox.frame.size;
        [rotatedViewBox release];
    
        UIGraphicsBeginImageContext(rotatedSize);
        CGContextRef bitmap = UIGraphicsGetCurrentContext();
    
    
        CGContextTranslateCTM(bitmap, rotatedSize.width, rotatedSize.height);
    
        CGContextRotateCTM(bitmap, DegreesToRadians(degrees));
    
    
        CGContextScaleCTM(bitmap, 1.0, -1.0);
        CGContextDrawImage(bitmap, CGRectMake(-image.size.width, -image.size.height, image.size.width, image.size.height), [image CGImage]);
    
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage;
    
    }
    

提交回复
热议问题