回答
您可以旋轉UIImageView
本身:
UIImageView *iv = [[UIImageView alloc] initWithImage:image];
iv.transform = CGAffineTransformMakeRotation(M_PI_2);
或者,如果你真的想改變形象,你可以使用代碼this answer,它的工作原理。
還有imageWithCIImage:scale:orientation
如果你想旋轉UIImage
不是UIImageView
與這些方向之一:
typedef enum {
UIImageOrientationUp,
UIImageOrientationDown, // 180 deg rotation
UIImageOrientationLeft, // 90 deg CW
UIImageOrientationRight, // 90 deg CCW
UIImageOrientationUpMirrored, // vertical flip
UIImageOrientationDownMirrored, // horizontal flip
UIImageOrientationLeftMirrored, // 90 deg CW then perform horizontal flip
UIImageOrientationRightMirrored, // 90 deg CCW then perform vertical flip
} UIImageOrientation;
要旋轉的像素可以使用以下。這創建了一個具有旋轉元數據的中間UIImage,並將其呈現爲寬高比尺寸調換的圖像上下文。將得到的圖像具有旋轉(即基本CGImage)的像素
- (UIImage*)rotateUIImage:(UIImage*)sourceImage clockwise:(BOOL)clockwise
{
CGSize size = sourceImage.size;
UIGraphicsBeginImageContext(CGSizeMake(size.height, size.width));
[[UIImage imageWithCGImage:[sourceImage CGImage] scale:1.0 orientation:clockwise ? UIImageOrientationRight : UIImageOrientationLeft] drawInRect:CGRectMake(0,0,size.height ,size.width)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
有可以傳遞爲取向參數,實現180度旋轉的其它可能的值和翻轉等
如果原始圖像是'UIImageOrientationRight',並且您在此處設置了'UIImageOrientationLeft',那麼生成的輸出將被壓扁以適應新的尺寸。 – ray 2016-04-29 17:26:36
UIImage *img = [UIImage [email protected]"aaa.png"];
UIImage *image = [UIImage imageWithCGImage:img.CGImage scale:1.0 orientation:UIImageOrientationRight];
隨着斯威夫特,你可以通過做一個圖像旋轉:
var image: UIImage = UIImage(named: "headerBack.png")
var imageRotated: UIImage = UIImage(CGImage: image.CGImage, scale:1, orientation: UIImageOrientation.UpMirrored)
這將旋轉任何給定的度數的圖像。
注意這個作品2倍和3倍視網膜以及
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees {
CGFloat radians = DegreesToRadians(degrees);
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.size.width, self.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(radians);
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;
UIGraphicsBeginImageContextWithOptions(rotatedSize, NO, [[UIScreen mainScreen] scale]);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
CGContextRotateCTM(bitmap, radians);
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;
}
這裏是@迅速版RyanG的Objective C代碼的一個擴展的UIImage:
extension UIImage {
func rotate(byDegrees degree: Double) -> UIImage {
let radians = CGFloat(degree*M_PI)/180.0 as CGFloat
let rotatedViewBox = UIView(frame: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))
let t = CGAffineTransform(rotationAngle: radians)
rotatedViewBox.transform = t
let rotatedSize = rotatedViewBox.frame.size
let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(rotatedSize, false, scale)
let bitmap = UIGraphicsGetCurrentContext()
CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
bitmap!.rotate(by: radians);
bitmap!.scaleBy(x: 1.0, y: -1.0);
CGContextDrawImage(bitmap, CGRectMake(-self.size.width/2, -self.size.height/2 , self.size.width, self.size.height), self.CGImage);
let newImage = UIGraphicsGetImageFromCurrentImageContext()
return newImage
}
}
用法image.rotate(degree)
。
謝謝Jason Crocker解決了我的問題。只有一個小小的修正,在這兩個位置互換的高度和寬度,並沒有出現失真,即
UIGraphicsBeginImageContext(CGSizeMake(size.width, size.height));
[[UIImage imageWithCGImage:[sourceImage CGImage] scale:1.0 orientation:clockwise ? UIImageOrientationRight : UIImageOrientationLeft] drawInRect:CGRectMake(0,0,size.width,size.height)];
我的問題不能由CGContextRotateCTM來解決,我不知道爲什麼。我的問題是,我正在將我的圖像傳輸到服務器,並一直顯示90度。通過將圖像複製到您在Mac上運行的MS Office程序,您可以輕鬆測試圖像是否在非蘋果世界中運行。
這就是我所做的,當我想改變圖像的方向(順時針旋轉90度)。
//Checking for the orientation ie, image taken from camera is in portrait or not.
if(yourImage.imageOrientation==3)
{
//Image is in portrait mode.
yourImage=[self imageToRotate:yourImage RotatedByDegrees:90.0];
}
- (UIImage *)image:(UIImage *)imageToRotate RotatedByDegrees:(CGFloat)degrees
{
CGFloat radians = degrees * (M_PI/180.0);
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0, image.size.height, image.size.width)];
CGAffineTransform t = CGAffineTransformMakeRotation(radians);
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;
UIGraphicsBeginImageContextWithOptions(rotatedSize, NO, [[UIScreen mainScreen] scale]);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(bitmap, rotatedSize.height/2, rotatedSize.width/2);
CGContextRotateCTM(bitmap, radians);
CGContextScaleCTM(bitmap, 1.0, -1.0);
CGContextDrawImage(bitmap, CGRectMake(-image.size.width/2, -image.size.height/2 , image.size.height, image.size.width), image.CGImage);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
旋轉後的圖像可以是大小> = 15MB(從我的經驗)的。所以你應該壓縮它並使用它。否則,您可能遇到導致內存壓力的崩潰。下面給出了用於壓縮的代碼。
NSData *imageData = UIImageJPEGRepresentation(yourImage, 1);
//1 - it represents the quality of the image.
NSLog(@"Size of Image(bytes):%d",[imageData length]);
//Here I used a loop because my requirement was, the image size should be <= 4MB.
//So put an iteration for more than 1 time upto when the image size is gets <= 4MB.
for(int loop=0;loop<100;loop++)
{
if([imageData length]>=4194304) //4194304 = 4MB in bytes.
{
imageData=UIImageJPEGRepresentation(yourImage, 0.3);
yourImage=[[UIImage alloc]initWithData:imageData];
}
else
{
NSLog(@"%d time(s) compressed.",loop);
break;
}
}
現在你yourImage
可用於任何地方.. 快樂編碼...
- 1. 如何旋轉UIImage或UIImageView?
- 2. Xamarin UIImage旋轉
- 3. UIImage旋轉軸
- 4. 旋轉UIImage?
- 5. UIImage旋轉
- 6. UIImage像素旋轉
- 7. 幫助旋轉UIImage
- 8. 旋轉UIImage問題
- 9. Xcode:UIImageView旋轉後如何獲得UIImage?
- 10. 如何旋轉90度的UIImage?
- 11. 如何在圖像中心旋轉UIImage
- 12. 如何旋轉UIImage點擊旋轉按鈕
- 13. iOS:如何旋轉CGImage並將其旋轉到UIImage上?
- 14. 旋轉UIImage的給予空白的UIImage
- 15. 在drawRect中旋轉UIImage
- 16. 裁剪旋轉的UIImage
- 17. 裁剪後UIImage旋轉
- 18. 旋轉UIImage的問題
- 19. 旋轉UIImage問題 - IOS/Swift
- 20. 旋轉和裁剪UIImage
- 21. UIImage旋轉自定義度
- 22. 旋轉的UIImage的MonoTouch
- 23. 在iPhone中旋轉UIImage
- 24. 旋轉UIImage自定義度
- 25. UIImage複製結束旋轉
- 26. UIImage在裁剪後旋轉
- 27. UIImageView旋轉我的UIImage
- 28. UIImage iPhone旋轉37.8度
- 29. 轉換UIScrollViewPoint到的UIImage + ZOOM +旋轉
- 30. 旋轉UIImage固定角度轉向
我做喜歡說,但是我有一個問題...我從旋轉圖像左上角,我怎麼能設置它從UIImage的中心旋轉圖像? – 2013-02-13 16:41:39
你可以玩'iv.layer.anchorPoint',但它的默認值是一個圖層的中心,所以我不確定它爲什麼會從左上角旋轉圖像。 – kovpas 2013-02-13 16:47:17
不,但我使用了您提供給我的鏈接中的代碼。所以這是CGContextRotateCTM – 2013-02-13 16:57:11