旋转NSImageCell

时间:2012-04-21 21:08:11

标签: objective-c nsmatrix applescript-objc nsimagecell

我正在开展一个项目,我的NSMatrix NSImageCells很大。我需要在他们的单元格中旋转特定的单个Images(或者甚至只是旋转单元格,因为它看起来会一样)。每个单元格和图像都是40x40的正方形,因此不必进行任何调整大小,因为我将坚持90度增量。问题是因为我使用NSImageCells而不是NSImageViews,我无法使用setFrameCenterRotation:轻松旋转图像。有谁知道我怎么能做到这一点?

1 个答案:

答案 0 :(得分:1)

对于仍在寻找方法的人来说,因为我在ASOC写这篇文章,所以我最终使用了这段代码:

on rotateImage_byAngle_(startingImage, angle)
    set rotated to current application's NSImage's alloc()'s initWithSize_({startingImage's |size|()'s height(), startingImage's |size|()'s |width|()})
    rotated's lockFocus()
    set transform to current application's NSAffineTransform's transform()
    transform's translateXBy_yBy_((rotated's |size|()'s |width|()) / 2, (rotated's |size|()'s height()) / 2)
    transform's rotateByDegrees_(angle)
    transform's translateXBy_yBy_(-(rotated's |size|()'s height()) / 2, -(rotated's |size|()'s |width|()) / 2)
    transform's concat()
    startingImage's drawAtPoint_fromRect_operation_fraction_({0, 0}, {{0, 0}, {0, 0}}, current application's NSCompositeCopy, 1)
    rotated's unlockFocus()
    return rotated
end rotateImage_byAngle_