Swift-如何写share safari camera

下面是实现 Share /Safari /Camera 过程


import UIKit

import SafariServices

import MessageUI


class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, MFMailComposeViewControllerDelegate {

    @IBOutlet weak var imageView: UIImageView!

    @IBOutlet weak var SharePressed: UIButton!

    

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

    }


    @IBAction func SharePressed(_ sender: UIButton) {

        guard let image = imageView.image else { return }

        let activityController = UIActivityViewController(activityItems: [image], applicationActivities: nil)

        activityController.popoverPresentationController?.sourceView = sender

        

        present(activityController, animated: true, completion: nil)

    }

    

    @IBAction func SafariPressed(_ sender: UIButton) {

        if let url = URL(string: "http://www.sample.net") {

            let safariViewController = SFSafariViewController(url: url)

            present(safariViewController,animated: true, completion: nil)

        }

        

    }

    

    @IBAction func CameraPressed(_ sender: UIButton) {

        

        let imagePicker = UIImagePickerController()

        imagePicker.delegate = self

        

        

        let alertController = UIAlertController(title: "Choice a photo", message: nil, preferredStyle: .actionSheet)

        

        //ipad使用,不加ipad上会崩溃

        if let popoverController = alertController.popoverPresentationController {

            popoverController.sourceView = sender

            popoverController.sourceRect = sender.bounds

        }

        

        

        //add cancel action

        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)

        alertController.addAction(cancelAction)

        

        //add camera action

        if UIImagePickerController.isSourceTypeAvailable(.camera) {

            

            let cameraAction = UIAlertAction(title: "Camera", style: .default, handler: { action in

                imagePicker.sourceType = .camera

                self.present(imagePicker, animated: true, completion: nil) })

            alertController.addAction(cameraAction)

        }

        //add photo library action

        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {

            let photolibraryAction = UIAlertAction(title: "Photo Library", style: .default, handler: { action in

                imagePicker.sourceType = .photoLibrary

                self.present(imagePicker,animated: true, completion: nil) })

             alertController.addAction(photolibraryAction)

            }

        //add all choices

        present(alertController, animated: true, completion: nil)

        

        

//        //image handler

//        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

//            if let selectedImage =

//                info[UIImagePickerControllerOriginalImage] as?  UIImage {

//                imageView.image = selectedImage

//                dismiss(animated: true,completion: nil)

//            }

//          }

       }

    //image handler

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

        if let selectedImage =

            info[UIImagePickerControllerOriginalImage] as UIImage {

            imageView.image = selectedImage

            dismiss(animated: true,completion: nil)

        }

    }

    @IBAction func EmailPressed(_ sender: UIButton) {

        if !MFMailComposeViewController.canSendMail() {

            print("Can not send mail!")

            return

        }

//        optional public func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?)

    }

    

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }



}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值