-1

I am new to objective-c and cocoa programming. I am trying to generate image which will be 128mm in height and 128mm in width with 300 DPI resolution.

NSString *image = [[NSImage alloc] initWithSize:NSMakeSize(1512, 756)];

In above line of code 1512 and 756 are treated as points. So I am not able to convert it to what I need. It is creating image with (3024 * 1512) size.

Can you please suggest something... Thanks in advance.

Here is the code which I tried

NSImage *image = [[NSImage alloc] initWithSize:NSMakeSize(2268, 2268)];
[image lockFocus];
// Draw Something
[image unlockFocus];
NSString* pathh = @"/Users/abcd/Desktop/Images/1234.bmp";

CGImageRef cgRef = [image CGImageForProposedRect:NULL
                                                 context:nil
                                                   hints:nil];
        NSBitmapImageRep *newRep = [[NSBitmapImageRep alloc]    initWithCGImage:cgRef];


        NSSize copySize; // To change to 600 DPI
        copySize.width = 600 * [newRep pixelsWide] / 72.0;
        copySize.height = 600 * [newRep pixelsWide] / 72.0;
        [newRep setSize:copySize];   //This input is not working
        NSData *pngData = [newRep representationUsingType:NSBMPFileType properties:nil];
        [pngData writeToFile:pathh atomically:YES];
A2212007
  • 11
  • 4

1 Answers1

0

Your question is somewhat confusing as you state you want a square image (128 x 128mm) and then attempt to construct a rectangular one (1512 x 756pts).

Guessing: it seems you may need to understand the difference between an NSImage and an NSImageRep and how they interact. Read Apple's Cocoa Drawing Guide, in particular the Images section. You may find the subsection Image Size and Resolution help to set the picture (no pun intended ;-)).

Another area you can read up on is printing - this often requires the generation of 300dpi images.

HTH

CRD
  • 52,522
  • 5
  • 70
  • 86
  • Thanks CRD for your inputs. My question is , I want to create a square image (128 x 128mm) with 300 DPI. Thats All. Rest things are all about what I tried. Can you please tell me how to create a square image (128 x 128mm) ? – A2212007 Dec 19 '15 at 03:00
  • What have you tried after reading the referenced and other material? Edit the question to show the code you produced etc. and what you're now stuck on. With that information somebody might be able to help you. – CRD Dec 19 '15 at 18:05
  • CRD, I have added what I tried. Can you please revisit ? – A2212007 Dec 22 '15 at 06:40
  • What does `NSImageRep` say about `size` and its relationship to `pixelsHigh` and `pixelsWide`? Also about its relationship to the enclosing `NSImage`'s `size`? (Hint: keep the two `size`s the same.) – CRD Dec 23 '15 at 20:45