2015-10-11 29 views
0

我試圖做一個應用程序,用戶可以從他們的圖像庫中選擇一個圖像作爲背景,然後選擇他們的徽標,他們也可以從他們的(照片)庫中選擇...如何覆蓋xcode中的圖像的圖像?

目前我還沒有找到可以幫助我的教程,主要是因爲用戶必須能夠設置自己的圖像而不是默認圖像。 此外,你們中的任何一個人也知道我可以擁有多個UIImagePickerControllers,因爲這段代碼同時影響兩個圖像而不是每個選擇器?

我用的/有:
我使用SWIFT
我使用的Xcode 7.0.1
這是 the storyboard i currently use

我的快捷文件:

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 

    @IBOutlet weak var logo: UIImageView! 
    @IBOutlet weak var bgimg: UIImageView! 

    override func viewDidLoad() { 
    super.viewDidLoad() 
    } 

    @IBAction func selectbg(sender: AnyObject) { 
    let bgPicker = UIImagePickerController() 
    bgPicker.delegate = self 
    bgPicker.sourceType = .PhotoLibrary 
    self.presentViewController(bgPicker, animated: true, completion: nil) 
    } 

    @IBAction func selectlogo(sender: AnyObject) { 
    let logoPicker = UIImagePickerController() 
    logoPicker.delegate = self 
    logoPicker.sourceType = .PhotoLibrary 
    self.presentViewController(logoPicker, animated: true, completion: nil) 
    } 

    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) { 
    logo.image = image 
    bgimg.image = image 
    self.dismissViewControllerAnimated(false, completion: nil) 
    } 


    @IBAction func addImage(sender: AnyObject) { 

    let ActionAlert = UIAlertController(title: nil, message: "What image do you want to add/edit?", preferredStyle: .ActionSheet) 

    let bgimage = UIAlertAction(title: "Background", style: .Default, handler: { (Alert:UIAlertAction!) -> Void in 
     print("Edit background image") 
    }) 
    let logo = UIAlertAction(title: "Logo", style: .Default) { (Alert:UIAlertAction!) -> Void in 
     print("Edit logo") 
    } 
    let cancel = UIAlertAction(title: "Cancel", style: .Cancel) { (Alert:UIAlertAction!) -> Void in 
     print("remove menu") 
    } 

    ActionAlert.addAction(bgimage) 
    ActionAlert.addAction(logo) 
    ActionAlert.addAction(cancel) 

    self.presentViewController(ActionAlert, animated: true, completion: nil) 
} 

}

如果我就不清楚了,我的問題,隨時問更多的信息

回答

5

爲每個圖像創建兩個的UIImageView小號並將前景一個作爲子視圖添加到父級。

事情是這樣的:

let bgimg = UIImage(named: "image-name") // The image used as a background 
let bgimgview = UIImageView(image: bgimg) // Create the view holding the image 
bgimgview.frame = CGRect(x: 0, y: 0, width: 500, height: 500) // The size of the background image 

let frontimg = UIImage(named: "front-image") // The image in the foreground 
let frontimgview = UIImageView(image: frontimg) // Create the view holding the image 
frontimgview.frame = CGRect(x: 150, y: 300, width: 50, height: 50) // The size and position of the front image 

bgimgview.addSubview(frontimgview) // Add the front image on top of the background 
+0

美麗。謝謝您的回答! – dinesharjani