我希望图像处于相同方向,即使设备方向发生变化,但需要根据设备方向旋转UIImageView
。类似于Snapchat。
当我旋转设备时,视图看起来像这样。 。但它应该是这样的图像 - >
以下是我用来旋转图像的功能。
- (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;
}
}
}
}
}
还有其他人遇到过同样的问题吗?我该如何解决这个问题?