• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Using CALayer's renderInContext: method with geometryFlipped
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Using CALayer's renderInContext: method with geometryFlipped


  • Subject: Using CALayer's renderInContext: method with geometryFlipped
  • From: Nial Giacomelli <email@hidden>
  • Date: Sat, 22 Oct 2011 11:09:30 +0100

I have a CALayer (containerLayer) that I'm looking to convert to a
NSBitmapImageRep before saving the data out as a flat file. containerLayer
has its geometryFlipped property set to YES, and this seems to be causing
issues. The PNG file that is ultimately generated renders the content
correctly, but doesn't seem to takes the flipped geometry into account. I'm
obviously looking for test.png to accurately represent the content shown to
the left.

For reference,here's a screenshot of the issue:
http://i.stack.imgur.com/VasZH.png. You'll see containerLayer on the left
and the generated .png file on the right.

- (NSBitmapImageRep *)exportToImageRep
{
    CGContextRef context = NULL;
    CGColorSpaceRef colorSpace;
    int bitmapByteCount;
    int bitmapBytesPerRow;

    int pixelsHigh = (int)[[self containerLayer] bounds].size.height;
    int pixelsWide = (int)[[self containerLayer] bounds].size.width;

    bitmapBytesPerRow = (pixelsWide * 4);
    bitmapByteCount = (bitmapBytesPerRow * pixelsHigh);

    colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
    context = CGBitmapContextCreate (NULL,
                                     pixelsWide,
                                     pixelsHigh,
                                     8,
                                     bitmapBytesPerRow,
                                     colorSpace,
                                     kCGImageAlphaPremultipliedLast);
    if (context == NULL)
    {
        NSLog(@"Failed to create context.");
        return nil;
    }

    CGColorSpaceRelease(colorSpace);
    [[[self containerLayer] presentationLayer] renderInContext:context];

    CGImageRef img = CGBitmapContextCreateImage(context);
    NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc]
initWithCGImage:img];
    CFRelease(img);

    return bitmap;
}

For reference, here's the code that actually saves out the generated
NSBitmapImageRep:

NSData *imageData = [imageRep representationUsingType:NSPNGFileType
properties:nil];
[imageData writeToFile:@"test.png" atomically:NO];
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Using CALayer's renderInContext: method with geometryFlipped
      • From: Graham Cox <email@hidden>
  • Prev by Date: Re: Exception when entering too big value in text field (with number formatter)
  • Next by Date: CFRunLoopObserver causes crash on NSView drag.
  • Previous by thread: Re: CAShapeLayer as a mask for CALayer: rounded corners are stretched
  • Next by thread: Re: Using CALayer's renderInContext: method with geometryFlipped
  • Index(es):
    • Date
    • Thread