UIImageView旋转而不在Image内部旋转

时间:2016-03-28 11:08:25

标签: ios objective-c uiimageview rotation cgaffinetransform

我希望图像处于相同方向,即使设备方向发生变化,但需要根据设备方向旋转UIImageView。类似于Snapchat。

如图This is my original image中所述。

当我旋转设备时,视图看起来像这样。 Image after rotation。但它应该是这样的图像 - > enter image description here

以下是我用来旋转图像的功能。

- (UIImage *) rotateUIImage:(UIImageOrientation)imageOrientation {
// No-op if the orientation is already correct
if (imageOrientation == UIImageOrientationUp) return self;

// We need to calculate the proper transformation to make the image upright.
// We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
CGAffineTransform transform = CGAffineTransformIdentity;

switch (imageOrientation) {
    case UIImageOrientationDown:
    case UIImageOrientationDownMirrored:
        transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height);
        transform = CGAffineTransformRotate(transform, M_PI);
        break;

    case UIImageOrientationLeft:
    case UIImageOrientationLeftMirrored:
        transform = CGAffineTransformTranslate(transform, self.size.width, 0);
        transform = CGAffineTransformRotate(transform, M_PI_2);
        break;

    case UIImageOrientationRight:
    case UIImageOrientationRightMirrored:
        transform = CGAffineTransformTranslate(transform, 0, self.size.height);
        transform = CGAffineTransformRotate(transform, -M_PI_2);
        break;
    case UIImageOrientationUp:
    case UIImageOrientationUpMirrored:
        break;
}

switch (imageOrientation) {
    case UIImageOrientationUpMirrored:
    case UIImageOrientationDownMirrored:
        transform = CGAffineTransformTranslate(transform, self.size.width, 0);
        transform = CGAffineTransformScale(transform, -1, 1);
        break;

    case UIImageOrientationLeftMirrored:
    case UIImageOrientationRightMirrored:
        transform = CGAffineTransformTranslate(transform, self.size.height, 0);
        transform = CGAffineTransformScale(transform, -1, 1);
        break;
    case UIImageOrientationUp:
    case UIImageOrientationDown:
    case UIImageOrientationLeft:
    case UIImageOrientationRight:
        break;
}

// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height,
                                         CGImageGetBitsPerComponent(self.CGImage), 0,
                                         CGImageGetColorSpace(self.CGImage),
                                         CGImageGetBitmapInfo(self.CGImage));
CGContextConcatCTM(ctx, transform);
switch (imageOrientation) {
    case UIImageOrientationLeft:
    case UIImageOrientationLeftMirrored:
    case UIImageOrientationRight:
    case UIImageOrientationRightMirrored:
        // Grr...
        CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage);
        break;

    default:
        CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage);
        break;
}

// And now we just create a new UIImage from the drawing context
CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImage imageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
return img;
}

当方向改变时,请调用以下方法

- (void)orientationChanged:(NSNotification *)notification
    UIInterfaceOrientation toInterfaceOrientation=  [[UIDevice currentDevice] orientation];
if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
   {
        if(imgPreview.imgOrientation != UIImageOrientationUp)
        {
            UIImage *imgPhoto =  [_bgImage rotateUIImage:imgPreview.imgOrientation];
            _bgImageView.image = _bgImage;
        }
        else{
            UIImage *imgPhoto =  [_bgImage rotateUIImage:_bgImage.imageOrientation];
            _bgImageView.image = imgPhoto;
        }
   }
    else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft
        || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
                   SCImageDisplayerViewController *imgPreview = (SCImageDisplayerViewController *)_owner;
        if(imgPreview.imgOrientation == UIImageOrientationUp){
            if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
            {
                UIImage *imgPhoto =  [_bgImage rotateUIImage:UIImageOrientationRight];
                _bgImageView.image = imgPhoto;
            }
            else{
                UIImage *imgPhoto =  [_bgImage rotateUIImage:UIImageOrientationLeft];
                _bgImageView.image = imgPhoto;
            }
        }
        else
        {
            if(imgPreview.imgOrientation == UIImageOrientationRight && (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight))
            {
                UIImage *imgPhoto =  [_bgImage rotateUIImage:_bgImage.imageOrientation];
                _bgImageView.image = imgPhoto;
            }
            else if(imgPreview.imgOrientation == UIImageOrientationLeft && (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft))
            {
                UIImage *imgPhoto =  [_bgImage rotateUIImage:UIImageOrientationRight];
                _bgImageView.image = imgPhoto;
            }
            else{
                if(isUpSideDown){
                    UIImage *imgPhoto =  [_bgImage rotateUIImage:UIImageOrientationUpMirrored];
                    _bgImageView.image = imgPhoto;
                }
                else{

                    UIImage *imgPhoto =  [_bgImage rotateUIImage:UIImageOrientationDownMirrored];
                    _bgImageView.image = imgPhoto;
                }
            }
        }
   }
}

还有其他人遇到过同样的问题吗?我该如何解决这个问题?

0 个答案:

没有答案