Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用CGContext组合UILabel和UIImage_Ios_Swift_Uiimage_Cgcontext - Fatal编程技术网

Ios 使用CGContext组合UILabel和UIImage

Ios 使用CGContext组合UILabel和UIImage,ios,swift,uiimage,cgcontext,Ios,Swift,Uiimage,Cgcontext,我无法将UILabel和UIImage组合成一个UIImage 我有一个ui标签,用户可以在ui图像(视图)上对其进行翻译、缩放和旋转 当我尝试使用CGContext、cgContextTranslateCm和cgContextRotateCm组合成一个UIImage时,标签旋转时的位置不正确。旋转正确,但位置不正确。我做错了什么 关于堆栈溢出,有几个答案讨论了()的某些方面,但它们没有考虑旋转因子 我已经附加了一个图像,你可以看到结果 func bake(image:UIImage, with

我无法将
UILabel
UIImage
组合成一个
UIImage

我有一个
ui标签
,用户可以在
ui图像
(视图)上对其进行翻译、缩放和旋转

当我尝试使用CGContext、cgContextTranslateCm和cgContextRotateCm组合成一个
UIImage
时,标签旋转时的位置不正确。旋转正确,但位置不正确。我做错了什么

关于堆栈溢出,有几个答案讨论了()的某些方面,但它们没有考虑旋转因子

我已经附加了一个图像,你可以看到结果

func bake(image:UIImage, withLabel label: UILabel, outputSize:CGSize) -> UIImage? {
    let inputSize:CGSize = image.size
    let scale = max(outputSize.width / inputSize.width, outputSize.height / inputSize.height)
    let scaledSize = CGSizeMake(inputSize.width * scale, inputSize.height * scale)
    let center = CGPointMake(outputSize.width / 2, outputSize.height / 2)
    let outputRect = CGRectMake(center.x - scaledSize.width/2, center.y - scaledSize.height/2, scaledSize.width, scaledSize.height)

    UIGraphicsBeginImageContextWithOptions(outputSize, true, 0.0)

    let context:CGContextRef = UIGraphicsGetCurrentContext()!
    CGContextSetInterpolationQuality(context, CGInterpolationQuality.High)
    image.drawInRect(outputRect)

    if let text = label.text {
        if text.characters.count > 0 {
            var range:NSRange? = NSMakeRange(0, text.characters.count)
            let drawPoint = CGPointMake(
                label.frame.origin.x / label.superview!.frame.width * outputSize.width,
                label.frame.origin.y / label.superview!.frame.height * outputSize.height)
            let originalFont = label.font

            label.font = UIFont(name: label.font!.fontName, size: label.font!.pointSize / label.superview!.frame.width * outputSize.width)
            let attributes = label.attributedText?.attributesAtIndex(0, effectiveRange: &range!)

            let angle = atan2(label.transform.b, label.transform.a)

            CGContextTranslateCTM(context, drawPoint.x, drawPoint.y)
            CGContextRotateCTM(context, angle)

            text.drawInRect(outputRect, withAttributes: attributes)

            label.font = originalFont
        }
    }

    let outputImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return outputImage
}

可能重复的答案有什么帮助吗?@Laffen得出的结果与我得到的结果相同。文本位置不正确。也许缩放位有一些问题,但我没有具体的想法。