Marek H
Marek H

Reputation: 5566

How to create UnsafePointer<CGRect> from existing CGRect

How to create UnsafePointer? Trying let mediaBoxPtr = UnsafePointer(mediaBox) but fails

func PDFImageData(filter: QuartzFilter?) -> NSData? {
    let pdfData = NSMutableData()
    let consumer = CGDataConsumerCreateWithCFData(pdfData);
    var mediaBox =  CGRect(x: 0, y: 0, width: bounds.size.width, height: bounds.size.height)
    let mediaBoxPtr : UnsafePointer<CGRect> = nil //???? I need CGRect(x:0, y:0, bounds.size.width, bounds.size.height)
    if let pdfContext = CGPDFContextCreate(consumer, mediaBoxPtr, nil) {
      filter?.applyToContext(pdfContext)}

Upvotes: 1

Views: 697

Answers (1)

Martin R
Martin R

Reputation: 539705

You don't have to create a pointer. Simply pass the address of the mediaBox variable as "inout argument" with &:

var mediaBox =  CGRect(x: 0, y: 0, width: bounds.size.width, height: bounds.size.height)
if let pdfContext = CGPDFContextCreate(consumer, &mediaBox, nil) {
    // ...
}

For more information and examples, see "Interacting with C APIs":

Mutable Pointers

When a function is declared as taking an UnsafeMutablePointer<Type> argument, it can accept any of the following:

  • ...
  • An in-out expression that contains a mutable variable, property, or subscript reference of type Type, which is passed as a pointer to the address of the mutable value.
  • ...

Upvotes: 5

Related Questions