Core Graphics drawing a bitmap


(Don Giovanni) #1

Hello All.

I'm trying to use Core Graphics to draw to a UIView. I'm purposely avoiding using UIKit drawing methods for UIView because I'd like to go deeper into drawing: i have a button whose push down implementation in the View Controller is:

let colorSpace = CGColorSpaceCreateDeviceRGB()

a = CGContext(data: nil, width: 100, height: 100, bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo:
        CGImageAlphaInfo.premultipliedLast.rawValue);
    a?.setFillColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 0.0);
    a?.fill(CGRect(x: 0, y: 0, width: 100, height: 100));

    let i = a?.makeImage();
    UIGraphicsPushContext(a!);

    let v1 = DGView(frame: CGRect(x: 0, y: 0, width: 500, height: 550));
    v1.tag = 100;
    v1.image = UIImage(cgImage: i!);
    self.view.addSubview(v1);
    self.view.viewWithTag(100)?.backgroundColor = UIColor.white;

when i push the button, a UIView subclass (DGView) pops up with white
  background. in the draw method of DGView, I added this code:

UIGraphicsPopContext();
    let c = UIGraphicsGetCurrentContext();
    c?.draw((c?.makeImage())!, in: CGRect(x: 0, y: 0, width: 100, height:

            100));

The problem is nothing is being drawn. Output should've been a red rectangle. I would really appreciate some guidance on this. Thanks.


(Quinn “The Eskimo!”) #2

swift-users isn’t really the right place for questions related to Apple frameworks. You might be better off asking this in a different context, such as the Cocoa Touch topic area on DevForums.

<https://forums.developer.apple.com/community/app-frameworks/cocoa-touch>

Share and Enjoy

···

On 23 Apr 2017, at 06:11, Don Giovanni via swift-users <swift-users@swift.org> wrote:

I'm trying to use Core Graphics to draw to a UIView.

--
Quinn "The Eskimo!" <http://www.apple.com/developer/>
Apple Developer Relations, Developer Technical Support, Core OS/Hardware