2016-11-18 40 views
0

目前,我可以在頂部保存帶有圖像的照片,但是頂部圖像不在預覽屏幕上顯示的座標上視圖。如何使用Swift正確地在照片上放置圖像視圖

fileprivate func mergeUIViews() -> UIImage? 
{ 
    let bottomImage = photo 
    let topImage = uiViewInstance.image 

    let bottomImageHeight = bottomImage.size.height 
    let bottomImageWidth = bottomImage.size.width 

    let topImageHeight = uiViewInstance.frame.height 
    let topImageWidth = uiViewInstance.frame.width 
    let topImageOrigin = uiViewInstance.frame.origin 

    let bottomImageSize = CGSize(width: bottomImageWidth, height: bottomImageHeight) 
    let topImageSize = CGSize(width: topImageWidth, height: topImageHeight) 

    // Merge images 

    UIGraphicsBeginImageContextWithOptions(bottomImageSize, false, 0.0) 

    bottomImage.draw(in: CGRect(origin: CGPoint.zero, size: bottomImageSize)) 
    topImage .draw(in: CGRect(origin: topImageOrigin, size: topImageSize)) // Where I believe the problem exists 
    let newImage = UIGraphicsGetImageFromCurrentImageContext() 

    UIGraphicsEndImageContext() 

    return newImage 
} 
+0

您打印過'uiViewInstance.frame.origin'嗎?它是你期望的CGPoint嗎? – Ryan

+0

@Ryan在故事板中,我將頂部圖像水平和垂直設置在中心進行測試。但是,一旦運行此代碼,合併的圖像被保存到照片庫,頂部圖像出現在左上角,水平和垂直間距都很小 – lifewithelliott

+0

愚蠢的問題,但是您檢查了newImage的外觀?它是否有可能具有(0,0)原點的頂部圖像? – dfd

回答

0

什麼工作,而不是使用實際拍攝照片的大小,則照片尺寸設置爲視圖控制器尺寸的視圖。這導致整個屏幕的期望輸出顯示合併的照片和重疊圖像的原點的適當點。

fileprivate func mergeUIViews() -> UIImage? 
{ 
    let bottomImage = photo 
    let topImage = uiViewInstance.image 

    let bottomImageSize = self.view.frame.size 

    let topImageHeight = uiViewInstance.frame.height 
    let topImageWidth = uiViewInstance.frame.width 
    let topImageOrigin = uiViewInstance.frame.origin 
    let topImageSize = uiViewInstance.frame.size 

    // Merge images 

    UIGraphicsBeginImageContextWithOptions(bottomImageSize, false, 0.0) 

    bottomImage.draw(in: CGRect(origin: CGPoint.zero, size: bottomImageSize)) 
    topImage .draw(in: CGRect(origin: topImageOrigin, size: topImageSize)) // Where I believe the problem exists 
    let newImage = UIGraphicsGetImageFromCurrentImageContext() 

    UIGraphicsEndImageContext() 

    return newImage 
} 
相關問題