将视图渲染为图像

时间:2015-11-13 14:07:47

标签: ios swift uiimageview uiimage

我正在使用相机拍照,当我按下某个按钮时,将调用createImage方法。在这里,我试图将containerView视图渲染到previewImageView中相机拍摄的图像中。但问题是相机中的图像尺寸为1080x1920containerView的尺寸不同,具体取决于设备。例如,在iphone 6上它将是375x503。我怎样才能确保这两个是如下面的快速插图一样精确地呈现在一起?

在我的代码中,containerView在底部角落非常小。

目前我的代码

func createImage() -> UIImage {

    //Get the taken image
    let fullSizeImage = previewImageView.image

    let newOverLayHeight = fullSizeImage!.size.width / self.containerView!.frame.width * self.containerView!.frame.height

    print(newOverLayHeight)

    //Render containerView to image
    UIGraphicsBeginImageContext(CGSizeMake(fullSizeImage!.size.width, newOverLayHeight));
    self.containerView!.layer.renderInContext(UIGraphicsGetCurrentContext()!)
    let overlayImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();


    //Merge these two images

    let newSize = CGSizeMake(fullSizeImage!.size.width, fullSizeImage!.size.height)
    UIGraphicsBeginImageContext( newSize )

    fullSizeImage!.drawInRect(CGRectMake(0,0,newSize.width,newSize.height))

    //draw overlayImage on top of fullsizeImage

    overlayImage.drawInRect(CGRectMake(0,fullSizeImage!.size.height - newOverLayHeight, overlayImage!.size.width,overlayImage.size.height), blendMode:CGBlendMode.Normal, alpha:1.0)

    let newImage = UIGraphicsGetImageFromCurrentImageContext()

    return newImage
}

我想要什么

enter image description here

0 个答案:

没有答案