【问题标题】:Create a UIImage after skewing text倾斜文本后创建 UIImage
【发布时间】:2013-12-06 01:37:15
【问题描述】:

我正在尝试从具有倾斜变换的 NSAttributedString 创建 UIImage。我可以制作一个 UILabel 或 UIImageView 并倾斜该视图,但不幸的是我需要在字符串倾斜后创建一个 UIImage 。我已经尝试了以下..

CGAffineTransform skewTransform = CGAffineTransformIdentity;
skewTransform.b = (M_1_PI / 2);

UIGraphicsBeginImageContextWithOptions(mSize, NO, 1.0);
UILabel* skewedLabel = [[UILabel alloc] init];
skewedLabel.attributedText = stringToBeSkewed;
CGContextSetTextMatrix(UIGraphicsGetCurrentContext(), skewTransform); 
skewedLabel.layer.affineTransform = skewTransform;

//    The following 3 calls haven't worked
//    [stringToBeSkewed drawInRect:inputRectParam.rect];
//    [skewedLabel.layer renderInContext:UIGraphicsGetCurrentContext()];
//    [skewedLabel.layer drawInContext:UIGraphicsGetCurrentContext()];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

我知道 CALayer 是 CoreAnimation 并且 CoreGraphics 无法识别这一点,但是有没有办法让倾斜的文本在上下文中被捕获并转换为 UIImage?

【问题讨论】:

    标签: ios uiimage core-graphics core-animation


    【解决方案1】:

    通过执行以下操作,我能够从文本中获取 UIImage,希望这对某人有所帮助。此外,这不是最干净的图像,所以任何关于锐化它的建议将不胜感激。

    UIFont* font = [UIFont fontWithName:@"(your font name)" size:60];
    CTFontRef skewFont = CTFontCreateWithName((__bridge CFStringRef)font.fontName, font.pointSize, NULL);
    
    // Create an attributed string for shadowed text
    CFStringRef skewKeys[] = { kCTFontAttributeName, kCTForegroundColorAttributeName };
    CFTypeRef skewValues[] = { skewFont, [UIColor colorWithRed:0 green:0 blue:0 alpha:.4].CGColor };
    CFDictionaryRef skewAttributes = CFDictionaryCreate(NULL, (const void **)&skewKeys, (const void **)&skewValues,
                                              sizeof(skewKeys) / sizeof(skewKeys[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
    CFStringRef theCFString = (__bridge CFStringRef)inputStringParam.string;
    CFAttributedStringRef skewString = CFAttributedStringCreate(NULL, theCFString, skewAttributes);
    CFRelease(skewAttributes);
    
    
    // create skew transform
    CGAffineTransform skewTransform = CGAffineTransformIdentity;
    skewTransform.b = (value to be skewed);
    
    // Draw the string
    CTLineRef skewLine = CTLineCreateWithAttributedString(skewString);
    
    CGContextSetTextMatrix(UIGraphicsGetCurrentContext(), CGAffineTransformMakeScale(1.0, -1.0));
    CGContextConcatCTM(UIGraphicsGetCurrentContext(), skewTransform);
    
    // draw and capture shadow image
    CGContextSetTextPosition(UIGraphicsGetCurrentContext(), inputDrawPointParam.point.x, inputDrawPointParam.point.y);
    CTLineDraw(skewLine, UIGraphicsGetCurrentContext());
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    CFRelease(skewLine);
    CFRelease(skewString);
    CFRelease(skewFont);
    

    【讨论】:

      猜你喜欢
      • 2020-04-12
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 2018-03-22
      • 1970-01-01
      • 2014-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多