4 Replies
      Latest reply on Dec 6, 2019 11:43 PM by Oxotoj
      Oxotoj Level 1 Level 1 (0 points)

        how do I get a blank image that I can draw lines and circles in and then save it to a jpeg file (and then open it later as a jpeg with ‘open’ )?

        could be a command line Xcode/swift app I believe.  Sounds easy but can’t find the swift way to do it (or any Xcode way)

        • Re: I want to use swift to. Open a 2d array or image buffer , pt a red pixel in the middle of the image/buffer, and save it as a jpg.  Any ideas examples of how to do that?
          Claude31 Level 8 Level 8 (7,255 points)

          You can subclass UIView,

          in the draw func, use the UIBezierPath to draw what you want.

           

          drawing is done with code like this:

             

                          let dotRect = CGRect(x: 100, y: 100, width: 6.0, height: 6.0)     // Of course position precisely where you want
                          let dotPath = UIBezierPath(rect: dotRect)
                          dotColor.setFill() 
                          dotPath.fill()

          After, convert the view to image with rendere, and save

           

          https://stackoverflow.com/questions/30696307/how-to-convert-a-uiview-to-an-image

              • Re: I want to use swift to. Open a 2d array or image buffer , pt a red pixel in the middle of the image/buffer, and save it as a jpg.  Any ideas examples of how to do that?
                eskimo Apple Staff Apple Staff (12,455 points)

                I can’t get Xcode 11.2.1 to recognize UIView as a defined type

                Right.  Claude31 assumed you were working on iOS but it seems that you’re working on macOS.

                To get you started on the Mac, take a look at this code:

                let bounds = NSRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0)
                let image = NSImage(size: bounds.size)
                image.lockFocus()
                NSColor.white.setFill()
                bounds.fill()
                let box = NSRect(x: 0.0, y: 0.0, width: 50.0, height: 50.0)
                NSColor.red.setFill()
                box.fill()
                image.unlockFocus()
                if let tiffData = image.tiffRepresentation(using: .lzw, factor: 0.0) {
                    print((tiffData as NSData).debugDescription)
                }

                If you take the hex data it prints, use a hex editor to paste it into a file, and give that file a .tiff extension, you’ll see a white square with a red square in one corner.

                NSImage is the ‘common currency’ for images on the Mac, so once you have this image you’ll be able to discover lots of different ways to work with it (adding it to an image view, drawing it in a graphics context, exporting it to JPEG, and so on).

                For more background on this, see the Cocoa Drawing Guide.

                Share and Enjoy

                Quinn “The Eskimo!”
                Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                let myEmail = "eskimo" + "1" + "@apple.com"

                  • Re: I want to use swift to. Open a 2d array or image buffer , pt a red pixel in the middle of the image/buffer, and save it as a jpg.  Any ideas examples of how to do that?
                    Oxotoj Level 1 Level 1 (0 points)

                    Thanks for the help;the key was, I think, (as usual??). Reading the .h files instead of relying on manuals,  which are great but never

                    have everything.   Btw. ‘Mighty Quinn ‘. Is a fun song I had forgotten about !  Great name!

                    ‘Here is the answer i was looking for ...not the colors I asked 4 but representative.

                     

                    NSSize isize = {400.0 , 400.0};

                            NSRect  nrect = {0.0,0.0,isize.width, isize.height};

                          

                          

                          

                            NSImage *image =  [[NSImage alloc] initWithSize: isize];

                            [image lockFocus];

                           

                          

                            float x_plot= 200, y_plot= 25;

                            float diameter1 = 20;

                            NSBezierPath * path3;

                            path3=[NSBezierPath bezierPathWithRect:nrect];

                            [NSColor.yellowColor set];

                            [path3 fill];

                          

                            [NSColor.blueColor set];

                            NSBezierPath * path2; // = [[NSBezierPath alloc] init] ; //rect: nrect];

                            path2=[NSBezierPath bezierPathWithOvalInRect:nrect];

                              [[NSColor blueColor] set];

                            [path2 fill];

                            NSRect myRect1 = NSMakeRect(x_plot, y_plot, diameter1, diameter1);

                               NSBezierPath *path1;

                               path1 = [NSBezierPath bezierPathWithOvalInRect:myRect1];

                               [[NSColor blackColor] set];

                               [path1 fill];

                          

                            [image unlockFocus];

                          

                            NSData *imageData = [image TIFFRepresentation];

                            NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData];

                            NSNumber *compressionFactor = [NSNumber numberWithFloat:0.9];

                            NSDictionary *imageProps = [NSDictionary dictionaryWithObject:compressionFactor

                                                                     forKey:NSImageCompressionFactor];

                          

                            imageData = [imageRep representationUsingType:NSBitmapImageFileTypeJPEGproperties:imageProps];

                            [imageData writeToFile:@"/Users/aaaaa/abc4defg.jpg" atomically:YES];