单击按钮时以编程方式将图像发送到新视图控制器

时间:2018-04-10 22:08:50

标签: swift uiimageview uiimage uistoryboardsegue uigraphicscontext

我正在尝试创建一个在“WebController”中加载UIWebView的应用程序,在UIWebView下面是一个按钮,它截取UIWebView的截图并将其保存到我的PhotoLibrary。我想将单击按钮时刚刚拍摄的照片发送到新的控制器“ViewImageController”,并插入到ViewImageController中的UIImageView中进行编辑或执行其他操作。我试图在我的WebController中使用segue将图像发送到我的ViewImageController。我看过很多关于在视图控制器之间传递图像的文章,但是我发现他们只处理了我没有使用的UIImagePickerController委托,因为我试图发送一张照片而不是拍摄照片,然后选择那张图片从ImagePicker发送。如果不清楚,请告诉我,以便我可以尝试更好地解释它。

WebController.swift

var sendImage:UIImage!
....
....
....
@IBAction func takePhotoBtn(_ sender: UIButton) {
    print("Screenshot")
    // = UIImage
    UIGraphicsBeginImageContext(webPageView.frame.size)
    if let aContext = UIGraphicsGetCurrentContext(){
        webPageView.layer.render(in: aContext)
    }
    var viewImage: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    UIImageWriteToSavedPhotosAlbum(viewImage!, nil, nil, nil)
    viewImage = sendImage
    self.performSegue(withIdentifier: "viewLargePhoto", sender: AnyObject.self)

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let viewImage: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    if segue.identifier == "viewLargePhoto" {
        var viewPhotoController = segue.destination as! ViewPhotoController
        //Line beneath is the trouble causer
        viewPhotoController.imageView = sendImage//Cannot assign value of type 'UIImage!' to type 'UIImageView!'
    }
}

ViewPhotoController.swift

@IBOutlet weak var imageView: UIImageView!

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

override func viewWillAppear(_ animated: Bool) {
    self.navigationController?.hidesBarsOnTap = true
    self.displayPhoto()
}
....
....
....
func displayPhoto(){
    let screenSize: CGSize = UIScreen.main.bounds.size
    let targetSize = CGSize(width: screenSize.width, height: screenSize.height)

    let imageManager = PHImageManager.default()
    imageManager.requestImage(for: self.photoAsset[self.index] as! PHAsset, targetSize: targetSize, contentMode: .aspectFit, options: nil, resultHandler: {(result,info)->Void in
        self.imageView.image = result
    })
}

1 个答案:

答案 0 :(得分:1)

在ViewPhotoController.swift中创建div#a { width: 100px; height: 100px; box-sizing: border-box; border: 1px solid gray; } @keyframes slide { to { height: var(--height); } } .slide { animation-name: slide; animation-duration: .4s; animation-timing-function: ease-in; animation-delay: 0s; animation-direction: normal; animation-iteration-count: 1; animation-fill-mode: forwards; animation-play-state: running; }

然后从WebController.swift传递您的图像  如下面的代码

var imageFromWebController: UIImage?

将该图像分配给ViewPhotoController中的imageView

if segue.identifier == "viewLargePhoto" {
        var viewPhotoController = segue.destination as! ViewPhotoController
        //Line beneath is the trouble causer
        viewPhotoController.imageFromWebController = sendImage
    }