다음 코드를 사용하여이 작업을 수행하고 있습니다. 문제는 이미지가 회전 할 때 이미지의 배율이 45 °에 가까울수록 더 커집니다.UIImage를 사용자 지정 각도로 회전하십시오.
이미지를 90 '및 그 배수로 설정하면 이미지 크기에 문제가 없습니다. 그러나 학위가 45 '와 그 배수에 가까워지면 이미지 크기가 눈에 띄게 줄어 듭니다. 이미지 크기를 변경하고 싶지 않습니다. 이 문제에 어떻게 접근해야합니까? 이미지가 회전 할 때 때문에 (완벽한 정사각형을 언급 할 때)보기의
- (UIImage *)imageRotatedByRadians:(CGFloat)radians img:(UIImage *)img
{
// calculate the size of the rotated view's containing box for our drawing space
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,img.size.width, img.size.height)];
UIView *nonrotatedViewBox = rotatedViewBox;
CGAffineTransform t = CGAffineTransformMakeRotation(radians);
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;
// Create the bitmap context
UIGraphicsBeginImageContext(nonrotatedViewBox.frame.size);
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, radians);
// Now, draw the rotated/scaled image into the context
CGContextScaleCTM(bitmap, 1.0, -1.0);
CGContextDrawImage(bitmap, CGRectMake(-img.size.width/2, -img.size.height/2, img.size.width, img.size.height), [img CGImage]);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
이미지를 회전해야합니까, 아니면 UIImageView를 회전 할 수 있습니까? 그러면 문제가 해결 될 수 있습니다. 또한 ... 뷰의 크기가 잘못되었으므로 너비와 높이를 2로 나눈 값으로 이미지를 그리는 것을 원하지 않습니다. 이미지가 회전 할 때 너비가 (완벽한 사각형을 나타낼 때) 너비가됩니다. 번 2 번 sqr (2). 그래서 너비와 높이가 10 인 경우, 너비 (45도 각도)가 14와 15 사이가됩니다 ... 그래서 컨텍스트가 10x10 영역에서 14x14 이미지를 그리게됩니다. –
복잡한 것을 경험할 수 있습니다. 기하학을 사용하여 완벽한 치수를 얻는 수학, 아니면 날개를 달아 말하기 | double percentRotated = (라디안 % M_PI_4)/M_PI_4; double newWidth = (percentRotated == 0? img.size.width : img.size.width * (1 + percentRotated)); 높이를 위해 똑같은 일을하십시오. –
이미지를 저장해야하기 때문에 이미지를 회전해야합니다. 내 이미지가 완벽한 사각형입니다 너비를 변경하려고합니다. – Esqarrouth