用textview渲染图像

时间:2015-08-27 15:22:58

标签: ios swift

我在图片上放置了一个textview。使用以下代码:

var previewImageView=UIImageView()
self.previewImageView.frame = CGRect(x:0, y:0, width:UIScreen.mainScreen().bounds.width, height:UIScreen.mainScreen().bounds.height)
self.view.addSubview(self.previewImageView)
textField = UITextView(frame: CGRect(x: 0, y: self.view.bounds.height/2 - 50, width: self.view.bounds.width, height: 36))
textField.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.6)
self.view.addSubview(textField)

效果很好。但我想用textview渲染这个图像。然后我将它保存到图库。

我该怎么做?

修改

我尝试了以下代码,但它只是渲染图像而不是textview:

//Setup the image context using the passed image.
UIGraphicsBeginImageContext(inImage.size)



//Put the image into a rectangle as large as the original image.
inImage.drawInRect(CGRectMake(0, 0, inImage.size.width, inImage.size.height))

// Creating a point within the space that is as bit as the image.
var rect: CGRect = CGRectMake(atPoint.x, atPoint.y, inImage.size.width, inImage.size.height)

//Now Draw the text into an image.
drawText.drawRect(rect)

// Create a new image out of the images we have created
var newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()

// End the context now that we have the image we need
UIGraphicsEndImageContext()

//And pass it back up to the caller.
return newImage

1 个答案:

答案 0 :(得分:3)

您可以使用CGGraphicsContext将整个视图渲染到UIImage中。以下是代码示例:

func imageWithView(view: UIView!) -> UIImage! {
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0)
    view.layer.renderInContext(UIGraphicsGetCurrentContext())

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

当你传递scale = 0.0时,它会占用你的屏幕比例。

使用示例:

let image = imageWithView(view)

保存图片的代码:

UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

有关UIGraphicsContext和图像保存的更多信息,请访问here

<强>更新

如果您只想渲染特定视图,则应使用 CALayer renderInContext 。当您使用 renderInContext 时,它不知道views.frame并在坐标系中绘制(0:0),因此您应该使用CGContextTranslateCTM来移动坐标系。

要只渲染这两个视图,您可以这样做:

UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0.0)
let context = UIGraphicsGetCurrentContext()
CGContextTranslateCTM(context, previewImageView.frame.origin.x, previewImageView.frame.origin.y)
previewImageView.layer.renderInContext(context)
CGContextTranslateCTM(context, textField.frame.origin.x - previewImageView.frame.origin.x, textField.frame.origin.y - previewImageView.frame.origin.y)
textField.layer.renderInContext(context)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

请根据需要设置opaque true / false。当我在操场上测试时,我使用的不是不透明的视图。

更新2:

适用于所有情况的代码:

func imageFromViews(views: [UIView]!, contextSize: CGSize!) -> UIImage {
    UIGraphicsBeginImageContextWithOptions(contextSize, false, 0.0)
    let context = UIGraphicsGetCurrentContext()

    for view in views {
        CGContextTranslateCTM(context, view.frame.origin.x, view.frame.origin.y)
        view.layer.renderInContext(UIGraphicsGetCurrentContext())
        CGContextTranslateCTM(context, -view.frame.origin.x, -view.frame.origin.y)
    }

    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return image
}