How to I rotate UIImageView by 90 degrees inside a UIScrollView with correct image size and scrolling?

前端 未结 5 1110
傲寒
傲寒 2021-02-02 04:37

I have an image inside an UIImageView which is within a UIScrollView. What I want to do is rotate this image 90 degrees so that it is in landscape by default, and set the initi

相关标签:
5条回答
  • 2021-02-02 04:45

    I believe the easiest way (and thread safe too) is to do:

    //assume that the image is loaded in landscape mode from disk
    UIImage * LandscapeImage = [UIImage imageNamed: imgname];
    UIImage * PortraitImage = [[UIImage alloc] initWithCGImage: LandscapeImage.CGImage
                                                         scale: 1.0
                                                   orientation: UIImageOrientationLeft];
    
    0 讨论(0)
  • 2021-02-02 04:49

    Maybe rotating the image itself fits your needs:

     UIImage* rotateUIImage(const UIImage* src, float angleDegrees)  {   
        UIView* rotatedViewBox = [[UIView alloc] initWithFrame: CGRectMake(0, 0, src.size.width, src.size.height)];
        float angleRadians = angleDegrees * ((float)M_PI / 180.0f);
        CGAffineTransform t = CGAffineTransformMakeRotation(angleRadians);
        rotatedViewBox.transform = t;
        CGSize rotatedSize = rotatedViewBox.frame.size;
        [rotatedViewBox release];
    
        UIGraphicsBeginImageContext(rotatedSize);
        CGContextRef bitmap = UIGraphicsGetCurrentContext();
        CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
        CGContextRotateCTM(bitmap, angleRadians);
    
        CGContextScaleCTM(bitmap, 1.0, -1.0);
        CGContextDrawImage(bitmap, CGRectMake(-src.size.width / 2, -src.size.height / 2, src.size.width, src.size.height), [src CGImage]);
    
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return newImage;
    }
    
    0 讨论(0)
  • 2021-02-02 04:49

    I was using the accepted answer for a while until we noticed that non-square rotations based on images taken directly from the camera seemed stretched (they were rotated as desired, just the frame width/height wasn't adjusted).

    Great explanation/post here from Trevor: http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

    In the end, it was a very simple import of Trevor's code which uses categories to add a resizedImage:interpoationQuality method to UIImage. So yeah, user beware, if it still works for you, great. But if it doesn't, I'd take a look at the library instead.

    0 讨论(0)
  • 2021-02-02 04:50

    There's a much easier solution that is also faster, just do this:

    - (void) imageRotateTapped:(id)sender
    {
        [UIView animateWithDuration:0.33f animations:^()
        {
            self.imageView.transform = CGAffineTransformMakeRotation(RADIANS(self.rotateDegrees += 90.0f));
            self.imageView.frame = self.imageView.superview.bounds; // change this to whatever rect you want
        }];
    }
    

    When the user is done, you will need to actually create a new rotated image, but that is very easy to do.

    0 讨论(0)
  • 2021-02-02 04:51

    Any calculations that you do based on the imageView's frame should probably be done before you apply any transformations to it. But I would actually suggest doing those calculations based on the size of the UIImage, not the UIImageView. Then set both the UIImageView's frame and the UIScrollView's contentSize based on that.

    Max's suggestion is a good one, although with a larger image it could be a performance killer. Are you displaying this image from your app's resources? If so, why not just rotate the images before you even build the app?

    0 讨论(0)
提交回复
热议问题