2015-07-19 8 views
0

У меня есть эта функция, которая принимает скриншот, что там в ImageViewКак добавить изображение пользовательского интерфейса в контекст, чтобы он включался в скриншот?

@IBAction func screenShotMethod(sender: UIButton) { 
     //Create the UIImage 

     UIGraphicsBeginImageContextWithOptions(imageView.frame.size, false, 0.0) 

     imageView.layer.renderInContext(UIGraphicsGetCurrentContext())   
     //I think here is where I could add the other imageView if I could name it properly. 

     let image = UIGraphicsGetImageFromCurrentImageContext() 


    } 

Я думаю, что мне нужно что-то вроде:

UIButton(named:"monkey.png").layer.renderInContext(UIGraphicsGetCurrentContext()) 

сразу после я добавить ImageView к контексту, но Im не уверен, как сделайте это правильно. Как вы можете видеть, я пытаюсь добавить imageView с именем monkey.png, который существует на main.storyboard.

Любые предложения?

ответ

1

Я не уверен, в чем проблема, почему ваш подход не сработал для вас, но добавив следующую строку вместо комментария, который вы поместили в код («Я думаю, вот где ...»)), делает то, что я понимаю, что вы пытаетесь достичь:

UIImageView(image: UIImage(named: "monkey.png")).layer.renderInContext(UIGraphicsGetCurrentContext()) 

так бы весь ваш метод выглядеть следующим образом:

@IBAction func screenShotMethod(sender: UIButton) { 
    UIGraphicsBeginImageContextWithOptions(imageView.frame.size, false, 0.0) 
    imageView.layer.renderInContext(UIGraphicsGetCurrentContext())   
    UIImageView(image: UIImage(named: "monkey.png")).layer.renderInContext(UIGraphicsGetCurrentContext())   
    let image = UIGraphicsGetImageFromCurrentImageContext() 
    // Do something with the image ;) 
}