如何在具有正确图像大小和滚动的UIScrollView内旋转UIImageView 90度?

SMS*_*dat 5 iphone uiscrollview uiimageview uiimage ios

我在UISmageView中有一个UIImageView内的图像.我想要做的是将此图像旋转90度,使其默认为横向,并设置图像的初始缩放,以便整个图像适合滚动视图,然后允许它缩放到100%并返回再次降至最小变焦.

这是我到目前为止:

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);
Run Code Online (Sandbox Code Playgroud)

问题是,如果我设置转换,滚动视图中不会显示任何内容.然而,如果我注释掉变换,除了图像不是我想要的横向方向外,一切都有效!

如果我应用转换并删除设置minimumZoomScale和zoomScale属性的代码,则图像以正确的方向显示,但是使用不正确的zoomScale并且似乎也没有正确设置contentSize属性 - 因为不滚动到左/右方向的图像边缘,但顶部和底部,但在边缘上方.

注意:正在从URL加载图像

Max*_*Max 19

也许旋转图像本身符合您的需求:

 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;
}
Run Code Online (Sandbox Code Playgroud)