【问题标题】:How to change image by 90 degree on iOS?如何在 iOS 上将图像更改 90 度?
【发布时间】:2023-04-08 03:10:01
【问题描述】:

当我从相机拍摄图像然后切换到另一个视图时,当我关闭该视图时,我的图像自动旋转了 -90 度,那么我怎样才能再次旋转到 90 度并将其恢复到原始位置?

【问题讨论】:

标签: ios objective-c


【解决方案1】:
imageView.transform = CGAffineTransformMakeRotation(3.14/2); 

应该有帮助

【讨论】:

    【解决方案2】:

    您必须使用 pi 值:

    ImageView.transform = CGAffineTransformMakeRotation(M_PI/2);

    【讨论】:

      【解决方案3】:

      这里有一个链接,你可以参考:

      您可以使用CGAffineTransform 来完成。

      根据您的问题,它足以满足您的要求。

      How to programmatically rotate image by 90 Degrees in iPhone?

      希望这会有所帮助。

      【讨论】:

        【解决方案4】:

        查看此功能缩放和旋转,它可能对您有所帮助

            image = [self scaleAndRotateImage:image];
        
        
        - (void)scaleAndRotateImage:(UIImage *)image
        {
            int kMaxResolution = 320; // Or whatever
        
            CGImageRef imgRef = image.CGImage;
        
            CGFloat width = CGImageGetWidth(imgRef);
            CGFloat height = CGImageGetHeight(imgRef);
        
            CGAffineTransform transform = CGAffineTransformIdentity;
            CGRect bounds = CGRectMake(0, 0, width, height);
            if (width > kMaxResolution || height > kMaxResolution) {
                CGFloat ratio = width/height;
                if (ratio > 1) {
                    bounds.size.width = kMaxResolution;
                    bounds.size.height = bounds.size.width / ratio;
                }
                else {
                    bounds.size.height = kMaxResolution;
                    bounds.size.width = bounds.size.height * ratio;
                }
            }
        
            CGFloat scaleRatio = bounds.size.width / width;
            CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
            CGFloat boundHeight;
            UIImageOrientation orient = image.imageOrientation;
            switch(orient) {
        
                case UIImageOrientationUp: //EXIF = 1
                    transform = CGAffineTransformIdentity;
                    break;
        
                case UIImageOrientationUpMirrored: //EXIF = 2
                    transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
                    transform = CGAffineTransformScale(transform, -1.0, 1.0);
                    break;
        
                case UIImageOrientationDown: //EXIF = 3
                    transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
                    transform = CGAffineTransformRotate(transform, M_PI);
                    break;
        
                case UIImageOrientationDownMirrored: //EXIF = 4
                    transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
                    transform = CGAffineTransformScale(transform, 1.0, -1.0);
                    break;
        
                case UIImageOrientationLeftMirrored: //EXIF = 5
                    boundHeight = bounds.size.height;
                    bounds.size.height = bounds.size.width;
                    bounds.size.width = boundHeight;
                    transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
                    transform = CGAffineTransformScale(transform, -1.0, 1.0);
                    transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
                    break;
        
                case UIImageOrientationLeft: //EXIF = 6
                    boundHeight = bounds.size.height;
                    bounds.size.height = bounds.size.width;
                    bounds.size.width = boundHeight;
                    transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
                    transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
                    break;
        
                case UIImageOrientationRightMirrored: //EXIF = 7
                    boundHeight = bounds.size.height;
                    bounds.size.height = bounds.size.width;
                    bounds.size.width = boundHeight;
                    transform = CGAffineTransformMakeScale(-1.0, 1.0);
                    transform = CGAffineTransformRotate(transform, M_PI / 2.0);
                    break;
        
                case UIImageOrientationRight: //EXIF = 8
                    boundHeight = bounds.size.height;
                    bounds.size.height = bounds.size.width;
                    bounds.size.width = boundHeight;
                    transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
                    transform = CGAffineTransformRotate(transform, M_PI / 2.0);
                    break;
        
                default:
                    [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];
        
            }
        
            UIGraphicsBeginImageContext(bounds.size);
        
            CGContextRef context = UIGraphicsGetCurrentContext();
        
            if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {
                CGContextScaleCTM(context, -scaleRatio, scaleRatio);
                CGContextTranslateCTM(context, -height, 0);
            }
            else {
                CGContextScaleCTM(context, scaleRatio, -scaleRatio);
                CGContextTranslateCTM(context, 0, -height);
            }
        
            CGContextConcatCTM(context, transform);
        
            CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
            UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
        
            [self setRotatedImage:imageCopy];
            //return imageCopy;
        }
        

        【讨论】:

          猜你喜欢
          • 2012-07-24
          • 1970-01-01
          • 2012-05-05
          • 1970-01-01
          • 1970-01-01
          • 2020-08-29
          • 2014-06-14
          • 2013-02-13
          • 2013-08-03
          相关资源
          最近更新 更多