gpt4 book ai didi

swift - 如何从现有 CGRect 创建 UnsafePointer

转载 作者:搜寻专家 更新时间:2023-11-01 06:38:16 25 4
gpt4 key购买 nike

如何创建UnsafePointer?尝试让 mediaBoxPtr = UnsafePointer(mediaBox) 但失败了

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)}

最佳答案

您不必创建指针。只需传递 mediaBox 的地址即可变量作为“输入参数”与& :

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

有关更多信息和示例,请参阅 "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.
  • ...

关于swift - 如何从现有 CGRect 创建 UnsafePointer<CGRect>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38897594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com