2013-02-13 131 views
15

我正在爲iPad開發iOS應用程序。有沒有辦法旋轉UIImage90º然後將其添加到UIImageView?我試了很多不同的代碼,但都沒有工作...如何旋轉UIImage

謝謝!

回答

18

您可以旋轉UIImageView本身:

UIImageView *iv = [[UIImageView alloc] initWithImage:image]; 
iv.transform = CGAffineTransformMakeRotation(M_PI_2); 

或者,如果你真的想改變形象,你可以使用代碼this answer,它的工作原理。

+0

我做喜歡說,但是我有一個問題...我從旋轉圖像左上角,我怎麼能設置它從UIImage的中心旋轉圖像? – 2013-02-13 16:41:39

+0

你可以玩'iv.layer.anchorPoint',但它的默認值是一個圖層的中心,所以我不確定它爲什麼會從左上角旋轉圖像。 – kovpas 2013-02-13 16:47:17

+0

不,但我使用了您提供給我的鏈接中的代碼。所以這是CGContextRotateCTM – 2013-02-13 16:57:11

1

這樣做的另一種方法是使用Core Graphics再次渲染UIImage。

一旦你有了上下文,使用CGContextRotateCTM

更多信息on this Apple Doc

7

還有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; 
17

要旋轉的像素可以使用以下。這創建了一個具有旋轉元數據的中間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度旋轉的其它可能的值和翻轉等

+0

如果原始圖像是'UIImageOrientationRight',並且您在此處設置了'UIImageOrientationLeft',那麼生成的輸出將被壓扁以適應新的尺寸。 – ray 2016-04-29 17:26:36

3
UIImage *img = [UIImage [email protected]"aaa.png"]; 
UIImage *image = [UIImage imageWithCGImage:img.CGImage scale:1.0 orientation:UIImageOrientationRight]; 
4

隨着斯威夫特,你可以通過做一個圖像旋轉:

var image: UIImage = UIImage(named: "headerBack.png") 
var imageRotated: UIImage = UIImage(CGImage: image.CGImage, scale:1, orientation: UIImageOrientation.UpMirrored) 
8

這將旋轉任何給定的度數的圖像。

注意這個作品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; 
} 
+0

這很好,但是有沒有辦法做到這一點,而不必創建一個UIView?我的理解是,UIView是一個相當重量級的對象。謝謝! – Mario 2016-12-29 21:55:46

+0

我發現一個CALayer可以用來代替UIView。創建圖層後,必須設置框架屬性。您還必須將CATransform3DMakeAffineTransform函數應用於CAAffineTransform對象(「t​​」)以設置圖層上的transform屬性。其他一切按原樣運作。我的理解是CALayer是一個更輕量級的對象。 – Mario 2016-12-30 16:24:13

4

這裏是@迅速版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)

1

謝謝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程序,您可以輕鬆測試圖像是否在非蘋果世界中運行。

1

這就是我所做的,當我想改變圖像的方向(順時針旋轉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可用於任何地方.. 快樂編碼...