#core-graphics #swift3 #cgcontext #core-foundation
#ядро-графика #swift3 #cgcontext #ядро-основа
Вопрос:
Я хочу нарисовать приписываемую строку с обрезкой rect, и я нашел функцию, написанную на Objective-C,
CFDictionaryRef PINCHFrameAttributesCreateWithClippingRect(CGRect clippingRect, CGAffineTransform transform)
{
CGPathRef clipPath = CGPathCreateWithRect(clippingRect, amp;transform);
CFDictionaryRef options;
CFStringRef keys[] = {kCTFramePathClippingPathAttributeName};
CFTypeRef values[] = {clipPath};
CFDictionaryRef clippingPathDict = CFDictionaryCreate(NULL,
(const void **)amp;keys, (const void **)amp;values,
sizeof(keys) / sizeof(keys[0]),
amp;kCFTypeDictionaryKeyCallBacks,
amp;kCFTypeDictionaryValueCallBacks);
CFTypeRef clippingArrayValues[] = { clippingPathDict };
CFArrayRef clippingPaths = CFArrayCreate(NULL, (const void **)clippingArrayValues, sizeof(clippingArrayValues) / sizeof(clippingArrayValues[0]), amp;kCFTypeArrayCallBacks);
CFStringRef optionsKeys[] = {kCTFrameClippingPathsAttributeName};
CFTypeRef optionsValues[] = {clippingPaths};
options = CFDictionaryCreate(NULL, (const void **)amp;optionsKeys, (const void **)amp;optionsValues,
sizeof(optionsKeys) / sizeof(optionsKeys[0]),
amp;kCFTypeDictionaryKeyCallBacks,
amp;kCFTypeDictionaryValueCallBacks);
CFRelease(clippingPathDict);
CFRelease(clippingPaths);
CGPathRelease(clipPath);
return options;
}
Но мне не удалось перевести его в Swift 3, кто-нибудь может мне помочь?
// Я пробовал нижеприведенные коды, он компилируется и запускается, но текст не выполняет клип:
let keys: [CFString] = [kCTFrameClippingPathsAttributeName]
let values: [CFTypeRef] = [clipPath]
let keysPointer = UnsafeMutablePointer<UnsafeRawPointer?>.allocate(capacity: keys.count)
keysPointer.initialize(to: keys)
let valuesPointer = UnsafeMutablePointer<UnsafeRawPointer?>.allocate(capacity: values.count)
valuesPointer.initialize(to: values)
let options = CFDictionaryCreate(kCFAllocatorDefault, keysPointer, valuesPointer, 1, nil, nil)
let framesetter = CTFramesetterCreateWithAttributedString(attributedSring)
let frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, attributedSring.length), rectPath.cgPath, options)
CTFrameDraw(frame, context)
Ответ №1:
Создайте словарь Swift и затем приведите его к CFDictionary
. Пример:
let d = [
kCGImageSourceShouldAllowFloat as String : true as NSNumber,
kCGImageSourceCreateThumbnailWithTransform as String : true as NSNumber,
kCGImageSourceCreateThumbnailFromImageAlways as String : true as NSNumber,
kCGImageSourceThumbnailMaxPixelSize as String : w as NSNumber
] as CFDictionary
Более подробная информация здесь:https://bugs.swift.org/browse/SR-2388