2010-05-18 6 views
15

이미지가있는 UIImageView가 있습니다. UIImageView의 변형 속성을 CGAffineTransformMakeRotation (angle)으로 설정하여 표시하기 전에 이미지를 회전했습니다. 여기서 angle은 라디안 단위의 각도입니다.회전 된 UIImageView에서 UIImage 만들기

내보기에서 볼 수있는 회전 된 버전에 해당하는 다른 UIImage를 만들 수 있기를 원합니다. 이미지가 중심에 대해

- (UIImage *) rotatedImageFromImageView: (UIImageView *) imageView 
{ 
    UIImage *rotatedImage; 

    // Get image width, height of the bounding rectangle 
    CGRect boundingRect = [self getBoundingRectAfterRotation: imageView.bounds byAngle:angle]; 

    // Create a graphics context the size of the bounding rectangle 
    UIGraphicsBeginImageContext(boundingRect.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // Rotate and translate the context 
    CGAffineTransform ourTransform = CGAffineTransformIdentity; 
    ourTransform = CGAffineTransformConcat(ourTransform, CGAffineTransformMakeRotation(angle)); 

    CGContextConcatCTM(context, ourTransform); 

    // Draw the image into the context 
    CGContextDrawImage(context, CGRectMake(0, 0, imageView.image.size.width, imageView.image.size.height), imageView.image.CGImage); 

    // Get an image from the context 
    rotatedImage = [UIImage imageWithCGImage: CGBitmapContextCreateImage(context)]; 

    // Clean up 
    UIGraphicsEndImageContext(); 
    return rotatedImage; 
} 

회전하지 않습니다하지만 : 나 이미지 컨텍스트를 회전하여, 거의 오전

나는 회전 된 이미지를 얻을. 내 모든 회전 변환을 연결하여 센터를 중심으로 회전 시키지만 아무 쓸모가 없도록 시도했습니다. 트릭을 놓치고 있습니까? 이미지가 아닌 컨텍스트를 회전시키고 있기 때문에 이것이 가능합니까?

지금이 작업을하기 위해 필사적으로 노력하십시오. 그러면 도움이 될 것입니다.

데이브

편집 : 내가 지금 여기있다, 내 boundingRect 코드를 여러 번 물어 봤는데 :

- (CGRect) getBoundingRectAfterRotation: (CGRect) rectangle byAngle: (CGFloat) angleOfRotation { 
    // Calculate the width and height of the bounding rectangle using basic trig 
    CGFloat newWidth = rectangle.size.width * fabs(cosf(angleOfRotation)) + rectangle.size.height * fabs(sinf(angleOfRotation)); 
    CGFloat newHeight = rectangle.size.height * fabs(cosf(angleOfRotation)) + rectangle.size.width * fabs(sinf(angleOfRotation)); 

    // Calculate the position of the origin 
    CGFloat newX = rectangle.origin.x + ((rectangle.size.width - newWidth)/2); 
    CGFloat newY = rectangle.origin.y + ((rectangle.size.height - newHeight)/2); 

    // Return the rectangle 
    return CGRectMake(newX, newY, newWidth, newHeight); 
} 
+0

이'getBoundingRectAfterRotation' 함수는 무엇입니까? – bmdhacks

+0

안녕하세요 @ bmdhacks getBoundingRectAfterRotation은 내가 만든 메서드이므로 Cocoa-Touch 클래스에서 찾을 수 없습니다. 인수로 rect와 angle을 취하고 주어진 각도만큼 회전 된 경우 원래 rect를 포함하는 새 CGRect를 반환합니다. –

+0

안녕하세요 @ bmdhacks 인기있는 수요로 인해 원래의 질문에 편집 방법을 추가했습니다. –

답변

22

확인 - 마지막에 내가 할 것으로 보인다. 정확성에 대한 모든 의견은 번역, 회전, 축척 및 그리기 rect 위치에서 오프셋을 필요로하므로 유용 할 것입니다. 코드는 여기에 있습니다 :

CGAffineTransform transform = CGAffineTransformIdentity; 
transform = CGAffineTransformTranslate(transform, boundingRect.size.width/2, boundingRect.size.height/2); 
transform = CGAffineTransformRotate(transform, angle); 
transform = CGAffineTransformScale(transform, 1.0, -1.0); 

CGContextConcatCTM(context, transform); 

// Draw the image into the context 
CGContextDrawImage(context, CGRectMake(-imageView.image.size.width/2, -imageView.image.size.height/2, imageView.image.size.width, imageView.image.size.height), imageView.image.CGImage); 

// Get an image from the context 
rotatedImage = [UIImage imageWithCGImage: CGBitmapContextCreateImage(context)]; 
+1

왜'transform = CGAffineTransformScale (transform, 1.0, -1.0);이 필요합니까? ? – Hlung

+3

CoreGraphics는 원점이 왼쪽 하단에있는 좌표 시스템을 사용합니다. UIKit 객체는 왼쪽 아래에 원점을 사용하고 화면 아래로 이동할 때 y 축이 증가합니다. 이 변환은 y 축을 뒤집어 이미지가 올바르게 그려지는지 확인합니다. –

+1

감사! 번역, 회전 및 크기 조정을 사용하여 컨텍스트에서 이미지를 그리는 데 어려움을 겪고있었습니다. 나는 UIImage의'drawInRect :'를 시도하고 문맥을 해석하지만 항상 왼쪽 상단 (0,0)을 중심으로 회전합니다. 당신의 솔루션이 유일한 솔루션입니다! :) – Hlung

관련 문제