이미지가있는 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);
}
이'getBoundingRectAfterRotation' 함수는 무엇입니까? – bmdhacks
안녕하세요 @ bmdhacks getBoundingRectAfterRotation은 내가 만든 메서드이므로 Cocoa-Touch 클래스에서 찾을 수 없습니다. 인수로 rect와 angle을 취하고 주어진 각도만큼 회전 된 경우 원래 rect를 포함하는 새 CGRect를 반환합니다. –
안녕하세요 @ bmdhacks 인기있는 수요로 인해 원래의 질문에 편집 방법을 추가했습니다. –