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

I have an image inside a UIImageView that is inside a UIScrollView. What I want to do is rotate this image 90 degrees so that it is in the landscape by default, and set the initial image scaling so that the whole image fits into the scroll, and then allows it to be scaled to 100% and back down to the minimum magnification .

This is what I have so far:

self.imageView.transform = CGAffineTransformMakeRotation(-M_PI/2);

float minimumScale = scrollView.frame.size.width  / self.imageView.frame.size.width;  
scrollView.minimumZoomScale = minimumScale;  
scrollView.zoomScale = minimumScale;  


scrollView.contentSize = CGSizeMake(self.imageView.frame.size.height,self.imageView.frame.size.width);

The problem is that if I set the conversion, nothing is displayed in scrollview. However, if I commented on the conversion, everything works, except that the image is not in the landscape orientation in which I want it to be!

, minimumZoomScale zoomScale, , , contentSize - / , , .

NB: URL

+5
5

, :

 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;
}
+19

, ( ):

//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];
+3

, imageView, , , - . UIImage, UIImageView. UIImageView, UIScrollView contentSize.

, . ? , , ?

+2

, , :

- (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
    }];
}

, , .

+2

, , , , ( , / ).

/ Trevor: http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

, Trevor, resizedImage:interpoationQuality UIImage. , , - , . , .

0

All Articles