1

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

BenJacob
  • 957
  • 10
  • 31
  • 2
    6,998,487 bytes may be the compressed size of the image on disk, but it's probably not the size in memory. 4110 * 2735 == 11,240,850 pixels * 4 bytes per pixel (for typical RGB image) == 44,963,400 bytes. It may be more (179,853,600 bytes) if `NSImage` is using floating-point components internally. That said, the number 4496342739800064 is clearly erroneous, but it may be due to some other bug. Have you logged `newSize` before allocating `rotatedImage`? – Ken Thomases May 02 '12 at 12:58
  • Ah, its calculating it at 27350000.469811 by 41100000.706005 – BenJacob May 02 '12 at 13:11
  • Using an imagerep to calculate the true size worked - thanks. – BenJacob May 02 '12 at 13:31

0 Answers0