NSImage를 비례 적으로 크기를 조절하고 90도 회전했지만 그 결과 이미지가 잘린 상태로 유지하려고합니다.drawInRect의 NSImage를 비례 적으로 비율 조정 및 회전
- (void) drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
NSImage* image = /* get image */;
CGRect drawRect = dirtyRect;
CGRect imageRect = [self proportionallyScale:image.size toSize:drawRect.size];
NSAffineTransform* rotation = [[NSAffineTransform alloc] init];
[rotation translateXBy:NSWidth(drawRect)/2 yBy:NSHeight(drawRect)/2];
[rotation rotateByDegrees:90];
[rotation translateXBy:-NSWidth(drawRect)/2 yBy:-NSHeight(drawRect)/2];
NSGraphicsContext* context = [NSGraphicsContext currentContext];
[context saveGraphicsState];
[rotation concat];
[image drawInRect:imageRect fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
[context restoreGraphicsState];
}
그리고 스케일링 로직 : 다음 NSView의 서브 클래스에서
가, 여기에 내가 현재하는 일이다 여기- (CGRect) proportionallyScale:(CGSize)fromSize toSize:(CGSize)toSize
{
CGPoint origin = CGPointZero;
CGFloat width = fromSize.width, height = fromSize.height;
CGFloat targetWidth = toSize.width, targetHeight = toSize.height;
float widthFactor = targetWidth/width;
float heightFactor = targetHeight/height;
CGFloat scaleFactor = std::min(widthFactor, heightFactor);
CGFloat scaledWidth = width * scaleFactor;
CGFloat scaledHeight = height * scaleFactor;
if (widthFactor < heightFactor)
origin.y = (targetHeight - scaledHeight)/2.0;
else if (widthFactor > heightFactor)
origin.x = (targetWidth - scaledWidth)/2.0;
return {origin, {scaledWidth, scaledHeight}};
}
이 적용되지 않은 회전 결과입니다 (이미지는 빨강, 사각형이 배경은도 임) 표시하도록 표현된다
그리고 난 [rotation concat]
있도록하여 변환을 적용했을 때
되는 문제가 그 이미지의 스케일링 및 회전 뷰포트의 크기가 아닌 입력 뷰 경계로 오프셋하여 계산한다 상대. 그러나 나는 이것을 올바르게 계산하는 논리를 생각해 낼 수 없다.
스케일링 된 이미지와 회전 된 이미지의 정확한 이미지 그리기 rect를 올바르게 계산하는 논리를 가진 사람이라면 누구든지 도와 주실 수 있습니까? 아니면 전체적인 문제에 대해 더 나은 접근법이 있습니까?