美文网首页
Swift 如何实现懒加载

Swift 如何实现懒加载

作者: jsone | 来源:发表于2020-02-27 15:06 被阅读0次

通过懒加载来实现从相册或拍照添加图片
1、声明懒加载属性imagePickerController

lazy var imagePickerController: UIImagePickerController = {
    let vc = UIImagePickerController.init()
    vc.delegate = self
    vc.allowsEditing = true
    return vc
}()

2、显示相册和拍照选项的SheetView

func showSheetView() -> Void {
    let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
    // 拍照选项
    let takingPicturesAction = UIAlertAction(title:"拍照", style: .default)
    {
        action in
        if UIImagePickerController.isSourceTypeAvailable(.camera) {
            self.imagePickerController.sourceType = .camera;
            self.present(self.imagePickerController, animated: true, completion: nil)
        } else {
            print("当前设备不支持拍照")
        }
    }
    // 相册选项
    let photoLibraryAction = UIAlertAction(title:"相册", style: .default)
    {
        action in
        self.imagePickerController.sourceType = .photoLibrary;
        self.present(self.imagePickerController, animated: true, completion: nil)
    }
    // 取消选项
    let cancelAction = UIAlertAction(title:"取消", style: .cancel, handler: nil)
    alertController.addAction(takingPicturesAction)
    alertController.addAction(photoLibraryAction)
    alertController.addAction(cancelAction)
    self.present(alertController, animated:true, completion:nil)
}

3、获取imagePickerController返回的图片

// MARK: - UIImagePickerControllerDelegate, UINavigationControllerDelegate
extension AddProductController : UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        // 获取照片
        let image : UIImage = info[UIImagePickerController.InfoKey.editedImage] as! UIImage
        addPictureButton.setImage(image, for: .normal)
        addPictureButton.setTitle("", for: .normal)
        self.dismiss(animated: true, completion: nil)
    }
}

相关文章

  • swift学习之懒加载

    在移动端开发过程之中,懒加载随处可见。在swift中,苹果推荐使用懒加载,如何实现懒加载呢? 关键字: lazy ...

  • Swift 如何实现懒加载

    通过懒加载来实现从相册或拍照添加图片1、声明懒加载属性imagePickerController 2、显示相册和拍...

  • Swift:)懒加载Lazy

    OC中,要实现懒加载是用到getter方法,例如 Swift懒加载差别就很大了,懒加载属性用lazy修饰,注意结尾...

  • swift学习09(懒加载)

    swift中是通过专门的关键字(lazy)来实现懒加载; 一、直接懒加载赋值 二、懒加载赋一个函数 三、懒加载赋一...

  • lazy

    swift中是通过专门的关键字(lazy)来实现懒加载; 直接懒加载赋值 懒加载赋一个函数 懒加载赋一个闭包 1、...

  • swift 懒加载

    swift 懒加载

  • swift 中 Lazy,deinit用法

    * swift中懒加载写法,懒加载在Swift中是一个闭包。 懒加载详细介绍看这里 * OC中的dealloc方法...

  • 学习Swift 所遇到的坑系列(2)

    1.如何在Swift中进行懒加载 使用ObjectC开发的同学,习惯性的使用懒加载去加载属性或者控件,那么到了Sw...

  • iOS swift 懒加载的使用

    记录一下swift的懒加载 OC回顾懒加载 - (UIButton *)but{ if (!_but){ _but...

  • Swift学习笔记

    [toc] 1. 常量、变量的定义 2. 懒加载 从oc中经常用的懒加载,在swift中该如何运用呢? 上述代码,...

网友评论

      本文标题:Swift 如何实现懒加载

      本文链接:https://www.haomeiwen.com/subject/jvnthhtx.html