2013-01-20 30 views
1

單個圖像我有兩個imageviews view1view2分別具有image1image2view1大於view2從兩個imageviews合併圖像轉換成在IOS

它的定位使view2位於view1內。 view2也是可拖動的。

我怎麼能寫一個代碼合併圖像中的這兩個圖像到一個圖像?

+0

可以澄清你的意思是合併?你想要image1粘貼在image2的頂部,沿着它,融入它等等? – ckhan

+0

你有沒有試過子視圖? – nhahtdh

回答

0

您可以使用UIGraphicsBeginImageContext合併兩張圖像。這是一個使用Swift 2.1.1編寫的小函數,它會拍攝兩張圖像並創建一張圖像。

func mergeImages (forgroundImage : UIImage, backgroundImage : UIImage) { 

    let bottomImage = forgroundImage 
    let topImage = backgroundImage 

    let size = backgroundImage.size 
    UIGraphicsBeginImageContext(size) 

    let areaSize = CGRect(x: 0, y: 0, width: size.width, height: size.height) 
    bottomImage.drawInRect(areaSize) 

    topImage.drawInRect(areaSize, blendMode: .Normal, alpha: 1.0) 

    let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext() 
    resultImageView.image = newImage 

    UIGraphicsEndImageContext() 
} 

你可以這樣調用它

mergeImages(yourImageViewOne.image!, backgroundImage: yourImageViewTwo.image!) // Call to mege images 

這裏是一個link進一步探索。

相關問題