-(NSImage *)roateSourceImage:(NSImage *) sourceImage ByDegrees:(CGFloat)degrees
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
NSRect imageBounds = {NSZeroPoint, [sourceImage size]};
NSBezierPath* boundsPath = [NSBezierPath
bezierPathWithRect:imageBounds];
NSAffineTransform* transform = [NSAffineTransform transform];
[transform rotateByDegrees:degrees];
[boundsPath transformUsingAffineTransform:transform];
NSRect rotatedBounds = {NSZeroPoint, [boundsPath bounds].size};
NSImage* rotatedImage = [[NSImage alloc]
initWithSize:rotatedBounds.size];
// center the image within the rotated bounds
imageBounds.origin.x = NSMidX(rotatedBounds) - (NSWidth
(imageBounds) / 2);
imageBounds.origin.y = NSMidY(rotatedBounds) - (NSHeight
(imageBounds) / 2);
// set up the rotation transform
transform = [NSAffineTransform transform];
[transform translateXBy:+(NSWidth(rotatedBounds) / 2) yBy:+
(NSHeight(rotatedBounds) / 2)];
[transform rotateByDegrees:degrees];
[transform translateXBy:-(NSWidth(rotatedBounds) / 2) yBy:-
(NSHeight(rotatedBounds) / 2)];
// draw the original image, rotated, into the new image
[rotatedImage lockFocus];
[transform set] ;
[sourceImage drawInRect:imageBounds fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
[rotatedImage unlockFocus] ;
[pool drain];
return rotatedImage ;
}
[Cocoa]_[初级]_[使用NSAffineTransfrom 对NSImage图片进行旋转]
最新推荐文章于 2021-03-14 15:32:21 发布
本文介绍了如何在Objective-C的Cocoa环境中,通过NSAffineTransform来实现NSImage图像的旋转操作。详细展示了`roateSourceImage:ByDegrees:`方法的实现过程,包括创建贝塞尔路径、设置旋转变换、创建新的旋转图像并绘制原图像。


1255

被折叠的 条评论
为什么被折叠?



