objective-cnsmatrixapplescript-objcnsimagecell

Rotating an NSImageCell


I'm working on a project where I have a large NSMatrix of NSImageCells. I need to rotate specific individual Images in their cells (or even just rotate the cells themselves since it will look the same way). Every cell and image is a 40x40 square, so there shouldn't have to be any resizing since I'll be sticking to 90 degree increments. The problem is that because I'm using NSImageCells instead of NSImageViews, I can't use setFrameCenterRotation: to easily rotate the images. Does anyone know how I can accomplish this?


Solution

  • For anyone still looking for a way to do this, since I'm writing this in ASOC, I ended up using this code:

    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_