버튼을 눌러 UIImageView를 회전하고 싶습니다.각도로 UIImageView 회전하기
@interface UIImage (CS_Extensions)
- (UIImage *)imageRotatedByRadians:(CGFloat)radians;
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees;
을하고하는 .m에서 :
#import "UIImage-Extensions.h"
UIImage *rotatedImage = [SplashItGroupPicker.image imageRotatedByDegrees:360/arrayCount];
SplashItGroupPicker.image = rotatedImage;
하고 "있는 UIImage-Extensions.h"에
내가 가진 :- (UIImage *)imageRotatedByRadians:(CGFloat)radians
{
return [self imageRotatedByDegrees:RadiansToDegrees(radians)];
}
- (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;
}
... 회전이 코드를 사용하고 잘 작동하지만 이미지를 회전하면 이미지도 작아지고 이유를 이해할 수 없습니다. 어떤 아이디어?
나는 그것에 대한 연구를하고 싶지 않았기 때문에 대답을 추가하지 않았다. 그러나 내 머리 꼭대기에서 내 생각은 CGContextDrawImage() 코드에있을 것이다. 원산지가 - (크기/2) 인 것처럼 보이지만 크기는 보통 크기입니다. 이미지가 회전 되었기 때문에 크기가 더 이상 일반 크기와 맞지 않습니다. 그래서 원래 크기보다 큰 rect이어야합니다. 회전을 설명합니다. 그 계산이 뭔지 확실하지 않습니다. 그러나 봐야 할 것이 있습니다. –
실제로, 그것은 알레산드로가 인용 한 코드에서 이루어졌습니다. 'rotatedViewBox' 부분을 확인하십시오. – Till
왜 그런 복잡한 방법을 사용하고 새 이미지를 만드나요? CGAffineTransformMakeRotation을 사용하여 이미지보기를 회전시킬 수 없습니까? – rdelmar