NSAffineTransform running out of memory

324 Views Asked by At

I am using NSAffineTransform to rotate/ reflect an NSImage and when using larger images i run into an error:

NSImage: Insufficient memory to allocate pixel data buffer of 4496342739800064 bytes

The image i am transforming here is 6,998,487 bytes at 4110px x 2735. Does NSAffineTransform really need this much memory to do this transformation or am i going wrong somewhere? Heres my rotate code:

-(NSImage *)rotateLeft:(NSImage *)img{
    NSImage *existingImage = img;
    NSSize existingSize;


    existingSize.width = existingImage.size.width;
    existingSize.height = existingImage.size.height;

    NSSize newSize = NSMakeSize(existingSize.height, existingSize.width);
    NSImage *rotatedImage = [[NSImage alloc] initWithSize:newSize];

    [rotatedImage lockFocus];


    NSAffineTransform *rotateTF = [NSAffineTransform transform];
    NSPoint centerPoint = NSMakePoint(newSize.width / 2, newSize.height / 2);

    [rotateTF translateXBy: centerPoint.x yBy: centerPoint.y];
    [rotateTF rotateByDegrees: 90];
    [rotateTF translateXBy: -centerPoint.y yBy: -centerPoint.x];
    [rotateTF concat];

    NSRect r1 = NSMakeRect(0, 0, newSize.height, newSize.width);
    [existingImage drawAtPoint:NSMakePoint(0,0)
        fromRect:r1
       operation:NSCompositeCopy fraction:1.0];

    [rotatedImage unlockFocus];

    return rotatedImage;
}

I am using ARC in my project. Thanks in advance, Ben

0

There are 0 best solutions below