> On 23 Jun 2017, at 23:37, Jens Alfke <j...@mooseyard.com> wrote:
> 
> 
>> On Jun 23, 2017, at 8:45 AM, Gerriet M. Denkmann <gerri...@icloud.com> wrote:
>> 
>> the image looks fine, but when I write subData to disk, it has 64 x 64 
>> pixels.
> 
> You’re probably running this on a computer with a Retina display, so NSImage 
> creates a 2x-resolution backing store by default.
Yes, you are right; I should have mentioned this.

> 
> Try creating an NSBitmapImageRep directly, then focus on it and draw.
> —Jens

> By making the NSBitMapRepresentation yourself, creating a context for it, 
> drawing to that.
> —Graham

You severely underestimate the depth of my ignorance. It took me half a day to 
fathom your (for me) cryptic remarks.
But thanks a lot for pointing me in the right direction!

This is what I did come up with (all error handling removed)  (things I am not 
quite sure are marked with “??”).
If there is anything I am doing wrong (or could do better) please tell me:


- (NSData *)subImagePngDataWithRect: (NSRect)subRect  useColour: (BOOL)useColour
{
        //      1.      create newSubRep (NSBitmapImageRep)
                
        NSString *colorSpaceName = useColour ? NSDeviceRGBColorSpace : 
NSDeviceWhiteColorSpace; //      ??
        NSInteger samplesPerPixel = useColour ? 4 : 2;  

        NSInteger pixelsWide = (NSInteger)ceil(subRect.size.width);
        NSInteger pixelsHigh = (NSInteger)ceil(subRect.size.height);
        NSInteger bitsPerSample = 8;    
        NSInteger bitsPerPixel = samplesPerPixel * bitsPerSample;       
        NSInteger bytesPerRow = pixelsWide * bitsPerPixel / 8;  
                
        //       there is another initializer with bitmapFormat - don't know 
what to use 
        NSBitmapImageRep *newSubRep =   [ [NSBitmapImageRep alloc]      
initWithBitmapDataPlanes:       NULL
                                                                                
                pixelsWide:                             pixelsWide
                                                                                
                pixelsHigh:                             pixelsHigh
                                                                                
                bitsPerSample:                  bitsPerSample 
                                                                                
                samplesPerPixel:                        samplesPerPixel
                                                                                
                hasAlpha:                               YES 
                                                                                
                isPlanar:                                       NO      //      
                                                                                
                                colorSpaceName:                         
colorSpaceName 
                                                                                
                bytesPerRow:                            bytesPerRow
                                                                                
                bitsPerPixel:                           bitsPerPixel
                                                                        ];


        //      2.      create context (NSGraphicsContext) from newSubRep and 
make it current
        
        NSGraphicsContext *context = [ NSGraphicsContext 
graphicsContextWithBitmapImageRep: newSubRep ];
        [ NSGraphicsContext setCurrentContext: context ];
        
        
        //      3.      draw allRep (NSBitmapImageRep) to currentContext
        
        NSBitmapImageRep *allRep = (NSBitmapImageRep 
*)self.image.representations.firstObject;  
        NSRect destRect = NSZeroRect; destRect.size = subRect.size;
        BOOL ok =       [ allRep        drawInRect:             destRect 
                                                fromRect:               subRect 
                                                operation:              
NSCompositingOperationCopy 
                                                fraction:                       
1
                                                respectFlipped: NO      //      
?? 
                                                hints:                  nil
                                ];              
                                
                                
        //      4. get pngData from newSubRep
        
        return [ newSubRep representationUsingType: NSPNGFileType  properties: 
@{} ];
}

Gerriet.


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to