Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

261
Views
Swift: comprender los punteros

Estoy tratando de crear un contexto de CoreGraphics usando la siguiente API de MacOS Quartz:

 CGContext.init?(_ url: CFURL, mediaBox: UnsafePointer<CGRect>?, _ auxiliaryInfo: CFDictionary?)

, pero tengo problemas con el concepto de punteros. Puedo iniciar el contexto así:

 var mypointer: UnsafeMutablePointer<CGRect>! mypointer.pointee = myCGRectangle var writeContext = CGContext.init(outURL, mediaBox: mypointer, nil)

Pero dice que pointee es una propiedad de solo obtener. He intentado:

 var mypointer: UnsafePointer<CGRect>! = UnsafePointer(myCGRectangle)

, basado en esto , pero no cannot convert CGRect to expected type , entre otros errores.

He examinado varias otras preguntas aquí sobre punteros, pero no puedo encontrar nada útil dentro de ellos.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Simplemente pase una referencia a su variable myCGRectangle , esto creará el UnsafeMutablePointer necesario:

 var writeContext = CGContext(outURL, mediaBox: &myCGRectangle, nil)

Asegúrese de que la variable del rectángulo se declare como var , de lo contrario, & no funcionará.

Se pueden encontrar más detalles en esta excelente página de documentación de Apple sobre cómo pasar punteros a funciones que los requieren.

over 4 years ago · Santiago Trujillo Report

0

Por lo general, no solo crea una variable de tipo UnsafeMutablePointer<CGRect> y la pasa a init(_:mediaBox:_:) . Prefiere pasar un rectángulo para el cual se mostrará PDF, por ejemplo

 var mediaBox = CGRect(x: 0, y: 0, width: 300, height: 500) ... var writeContext = CGContext(outURL, mediaBox: &mediaBox, nil)

(incluso eso es una simplificación, ya que crea un rectángulo directamente. Por lo general, tiene un rectángulo de la vista en la que desea mostrar el PDF, por lo que pasará ese rectángulo).

También puede pasar nil a init(_:mediaBox:_:) . En cuyo caso, como dicen los documentos:

Si pasa NULL, CGPDFContextCreateWithURL usa un tamaño de página predeterminado de 8,5 por 11 pulgadas (612 por 792 puntos).

Entonces, si ese rectángulo se ajusta a sus necesidades, simplemente pase nil (más simple).

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!