How to rotate UIImage

后端 未结 10 736
既然无缘
既然无缘 2021-01-03 23:03

I\'m developing an iOS app for iPad. Is there any way to rotate a UIImage 90º and then add it to a UIImageView? I\'ve tried a lot of different codes but none worked...

相关标签:
10条回答
  • 2021-01-03 23:41

    There is also imageWithCIImage:scale:orientation if you wanted to rotate the UIImage not the UIImageView

    with one of these orientations:

    typedef enum {
       UIImageOrientationUp,
       UIImageOrientationDown,   // 180 deg rotation
       UIImageOrientationLeft,   // 90 deg CW
       UIImageOrientationRight,   // 90 deg CCW
       UIImageOrientationUpMirrored,    // vertical flip
       UIImageOrientationDownMirrored,  // horizontal flip
       UIImageOrientationLeftMirrored,  // 90 deg CW then perform horizontal flip
       UIImageOrientationRightMirrored, // 90 deg CCW then perform vertical flip
    } UIImageOrientation;
    
    0 讨论(0)
  • 2021-01-03 23:41

    With Swift, you can rotate an image by doing:

    var image: UIImage = UIImage(named: "headerBack.png")
    var imageRotated: UIImage = UIImage(CGImage: image.CGImage, scale:1, orientation: UIImageOrientation.UpMirrored)
    
    0 讨论(0)
  • 2021-01-03 23:41

    This is what i've done when i wanted to change the orientation of an image (rotate 90 degree clockwise).

    //Checking for the orientation ie, image taken from camera is in portrait or not.
    if(yourImage.imageOrientation==3)
    {
        //Image is in portrait mode.
        yourImage=[self imageToRotate:yourImage RotatedByDegrees:90.0];
    }
    
    - (UIImage *)image:(UIImage *)imageToRotate RotatedByDegrees:(CGFloat)degrees
    {
        CGFloat radians = degrees * (M_PI / 180.0);
    
        UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0, image.size.height, image.size.width)];
        CGAffineTransform t = CGAffineTransformMakeRotation(radians);
        rotatedViewBox.transform = t;
        CGSize rotatedSize = rotatedViewBox.frame.size;
    
        UIGraphicsBeginImageContextWithOptions(rotatedSize, NO, [[UIScreen mainScreen] scale]);
        CGContextRef bitmap = UIGraphicsGetCurrentContext();
    
        CGContextTranslateCTM(bitmap, rotatedSize.height / 2, rotatedSize.width / 2);
    
        CGContextRotateCTM(bitmap, radians);
    
        CGContextScaleCTM(bitmap, 1.0, -1.0);
        CGContextDrawImage(bitmap, CGRectMake(-image.size.width / 2, -image.size.height / 2 , image.size.height, image.size.width), image.CGImage );
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return newImage;
    }
    

    The rotated image may be of size >= 15MB (from my experience). So you should compress it and use it. Otherwise, you may met with crash causing memory pressure. Code I used for compressing is given below.

    NSData *imageData = UIImageJPEGRepresentation(yourImage, 1);
    //1 - it represents the quality of the image.
    NSLog(@"Size of Image(bytes):%d",[imageData length]);
    //Here I used a loop because my requirement was, the image size should be <= 4MB.
    //So put an iteration for more than 1 time upto when the image size is gets <= 4MB.
    for(int loop=0;loop<100;loop++)
    {
        if([imageData length]>=4194304)  //4194304 = 4MB in bytes.
        {
            imageData=UIImageJPEGRepresentation(yourImage, 0.3);
            yourImage=[[UIImage alloc]initWithData:imageData];
        }
        else
        {
            NSLog(@"%d time(s) compressed.",loop);
            break;
        }
    }
    

    Now your yourImage can be used for anywhere.. Happy coding...

    0 讨论(0)
  • 2021-01-03 23:45

    To rotate the pixels you can use the following. This creates an intermediate UIImage with rotated metadata and renders it into a image context with width/height dimensions transposed. The resulting image has the pixels rotated (i.e the underlying CGImage)

    - (UIImage*)rotateUIImage:(UIImage*)sourceImage clockwise:(BOOL)clockwise
    {
        CGSize size = sourceImage.size;
        UIGraphicsBeginImageContext(CGSizeMake(size.height, size.width));
        [[UIImage imageWithCGImage:[sourceImage CGImage] scale:1.0 orientation:clockwise ? UIImageOrientationRight : UIImageOrientationLeft] drawInRect:CGRectMake(0,0,size.height ,size.width)];
        UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return newImage;
    }
    

    There are other possible values that can be passed for the orientation parameter to achieve 180 degree rotation and flips etc.

    0 讨论(0)
  • 2021-01-03 23:47

    Here is the swift version of @RyanG's Objective C code as an extension to UIImage:

    extension UIImage {
    
        func rotate(byDegrees degree: Double) -> UIImage {
            let radians = CGFloat(degree*M_PI)/180.0 as CGFloat
            let rotatedViewBox = UIView(frame: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))
            let t = CGAffineTransform(rotationAngle: radians)
            rotatedViewBox.transform = t
            let rotatedSize = rotatedViewBox.frame.size
            let scale = UIScreen.main.scale
            UIGraphicsBeginImageContextWithOptions(rotatedSize, false, scale)
            let bitmap = UIGraphicsGetCurrentContext()
            CGContextTranslateCTM(bitmap, rotatedSize.width / 2, rotatedSize.height / 2);
    
            bitmap!.rotate(by: radians);
    
            bitmap!.scaleBy(x: 1.0, y: -1.0);
            CGContextDrawImage(bitmap, CGRectMake(-self.size.width / 2, -self.size.height / 2 , self.size.width, self.size.height), self.CGImage );
    
            let newImage = UIGraphicsGetImageFromCurrentImageContext()
    
            return newImage
        }
    
    }
    

    The usage is image.rotate(degree).

    0 讨论(0)
  • 2021-01-03 23:47
    UIImage *img = [UIImage imageWithName@"aaa.png"];
    UIImage *image = [UIImage imageWithCGImage:img.CGImage scale:1.0 orientation:UIImageOrientationRight];
    
    0 讨论(0)
提交回复
热议问题