所以對於以圖像的時間採取存儲設備的方向並將它傳遞給下面的參數 這裏只是給出任何名稱方法的方法和傳遞參數orientation
switch (orientation) {
case UIDeviceOrientationPortrait:
[featureLayer setAffineTransform:CGAffineTransformMakeRotation(DegreesToRadians(0.))];
break;
case UIDeviceOrientationPortraitUpsideDown:
[featureLayer setAffineTransform:CGAffineTransformMakeRotation(DegreesToRadians(180.))];
break;
case UIDeviceOrientationLandscapeLeft:
[featureLayer setAffineTransform:CGAffineTransformMakeRotation(DegreesToRadians(90.))];
break;
case UIDeviceOrientationLandscapeRight:
[featureLayer setAffineTransform:CGAffineTransformMakeRotation(DegreesToRadians(-90.))];
break;
case UIDeviceOrientationFaceUp:
case UIDeviceOrientationFaceDown:
default:
break; // leave the layer in its last known orientation
}
和宏我在這裏DegreesToRadians()
採用的是如下
static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI/180;};
這肯定會工作。
編碼快樂:)
編輯
如果上面的代碼無法正常工作,然後用這一個
@interface UIImage (RotationMethods)
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees;
@end
@implementation UIImage (RotationMethods)
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees
{
// calculate the size of the rotated view's containing box for our drawing space
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width, self.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;
// Create the bitmap context
UIGraphicsBeginImageContext(rotatedSize);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
// Move the origin to the middle of the image so we will rotate and scale around the center.
CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
// // Rotate the image context
CGContextRotateCTM(bitmap, DegreesToRadians(degrees));
// Now, draw the rotated/scaled image into the context
CGContextScaleCTM(bitmap, 1.0, -1.0);
CGContextDrawImage(bitmap, CGRectMake(-self.size.width/2, -self.size.height/2, self.size.width, self.size.height), [self CGImage]);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
@end
,然後調用上面的函數如下
switch (orientation) {
case UIDeviceOrientationPortrait:
image = [image imageRotatedByDegrees:0];
break;
case UIDeviceOrientationPortraitUpsideDown:
image = [image imageRotatedByDegrees:180];
break;
case UIDeviceOrientationLandscapeLeft:
image = [image imageRotatedByDegrees:-90];
break;
case UIDeviceOrientationLandscapeRight:
image = [image imageRotatedByDegrees:90];
break;
case UIDeviceOrientationFaceUp:
case UIDeviceOrientationFaceDown:
default:
break; // leave the layer in its last known orientation
}
如果圖像不在請求中紅方向然後將90加到所有上述imageRotatedByDegrees
的論點(即,如果它是0,那麼它將是0 + 90)或按照你的要求。
編輯1
UIDeviceOrientation curDeviceOrientation = [[UIDevice currentDevice] orientation];
你的圖像是否旋轉到某個特定的程度? –
所以你必須讓他們再旋轉-90再顯示這將解決你的問題,如果你想要如何做到這一點,只是要求我的幫助 –
如果你需要代碼它只是要求它我會給你它的答案 –