2017-06-05 8 views
2

私のコードは今、画像を1つだけ保存しています。私は最初の画像の上の左上隅に小さな画像を保存したいと思います。大きなことは、写真ギャラリーで一緒に保存することです。 2枚の画像が重ねて表示されます。2枚の画像を重ねて保存する方法(swift3)

import UIKit 

class ViewController: UIViewController { 


let imageView = UIImageView(frame: CGRect(x: 50, y: 50, width: 300, height: 300)) 
let imageView2 = UIImageView(frame: CGRect(x:200, y: 50, width: 100, height: 100)) 



override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    let image = UIImage(named: "wall")! 
    imageView.image = image 


    let image2 = UIImage(named: "pic")! 
    imageView2.image = image2 

    imageView.addSubview(imageView2) 


    imageView2.image = image2 



    imageView.addSubview(imageView2) 

    let topImage = UIImage(named: "wall") 
    let bottomImage = UIImage(named: "pic") 

    let size = CGSize(width: topImage!.size.width, height: topImage!.size.height + bottomImage!.size.height) 
    UIGraphicsBeginImageContextWithOptions(size, false, 0.0) 
    topImage!.draw(in: CGRect(x: 0, y: topImage!.size.height, width: size.width, height: bottomImage!.size.height)) 
    bottomImage!.draw(in: CGRect(x: 0, y: topImage!.size.height, width: size.width, height: bottomImage!.size.height)) 

    //your new Image 
    let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()! 
        UIGraphicsEndImageContext() 

} 

@IBAction func press(_ sender: Any) { 
    UIImageWriteToSavedPhotosAlbum(imageView.image!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil) 
} 
func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) { 
    if let error = error { 
     // we got back an error! 
     let ac = UIAlertController(title: "Save error", message: error.localizedDescription, preferredStyle: .alert) 
     ac.addAction(UIAlertAction(title: "OK", style: .default)) 
     present(ac, animated: true) 
    } else { 
     let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .alert) 
     ac.addAction(UIAlertAction(title: "OK", style: .default)) 
     present(ac, animated: true) 
    }}} 

pic

答えて

0

このコードのすべては、イメージビューに追加するさまざまな機能になります。

 let bottomImage:UIImage = UIImage(named: "backdrop")! 
     let topImage:UIImage = UIImage(named:"wall")! 
     let topImage2:UIImage = UIImage(named:"wall")! 
     let tpp:UIImage = letImageVIEW.image! 
        let right:UIImage = rightIMAGEVIEW.image! 

     // Change here the new image size if you want 
     let newSize = CGSize(width: bottomImage.size.width, height: bottomImage.size.height ) 
     let newSize2 = CGSize(width: bottomImage.size.width, height: bottomImage.size.height) 
     UIGraphicsBeginImageContextWithOptions(newSize, false, bottomImage.scale) 
     //  bottomImage.draw(in: CGRect(x: 0,y: 0,width: newSize.width,height: newSize.height)) 
       tpp.draw(in: CGRect(x: 0,y: 0,width: newSize2.width,height: newSize2.height), blendMode:CGBlendMode.normal, alpha:1.0) 
       right.draw(in: CGRect(x: 0,y: 0,width: newSize2.width,height: newSize2.height), blendMode:CGBlendMode.normal, alpha:0.2) 

     topImage.draw(in: CGRect(x: 0,y: 0,width: newSize.width/2,height: newSize.height/2), blendMode:CGBlendMode.normal, alpha:1.0) 
     topImage2.draw(in: CGRect(x: 50,y: 0,width: newSize2.width/4,height: newSize2.height/4), blendMode:CGBlendMode.normal, alpha:1.0) 





     let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()! 
     UIGraphicsEndImageContext() 
     finalImage.image = newImage 
0
let imageView2 = UIImageView(frame: CGRect(x:200, y: 50, width: 100, height:100)) 
    let image2 = UIImage(named: "wallsmallImage")! 
    imageView2.image = image2 
    imageView.addSub(imageView2) 

#Now merge both images 
     let image = UIImage(named: "wall")! 
     imageView.image = image 
     let image2 = UIImage(named: "pic")! 
     imageView2.image = image2 

     imageView.addSubview(imageView2) 

     let topImage = UIImage(named: "wall") 
     let bottomImage = UIImage(named: "pic") 

     let size = CGSize(width: topImage!.size.width, height: topImage!.size.height + bottomImage!.size.height) 
     UIGraphicsBeginImageContextWithOptions(size, false, 0.0) 
     topImage!.draw(in: CGRect(x: 0, y: topImage!.size.height, width: size.width, height: bottomImage!.size.height)) 
     bottomImage!.draw(in: CGRect(x: 0, y: topImage!.size.height, width: size.width, height: bottomImage!.size.height)) 

     //your new Image 
     let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()! 
     UIGraphicsEndImageContext() 
+0

あなたのコードを追加しましたが、まだ1つの画像しか保存されていません。あなたの提案したコードで私の質問を編集しました。 –

+0

こんにちは@SamBurns私は両方の画像をマージしてギャラリーに保存する必要があると思います... –

+0

@SamBurns私は更新されたコードを確認してください –

関連する問題