热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

iOS11-始终打开照片库,即使源类型也从.photoLibrary更改为.camera

如何解决《iOS11-始终打开照片库,即使源类型也从.photoLibrary更改为.camera》经验,为你挑选了1个好方法。

该代码在iOS 10及更低版本中完美运行.但是,在iOS 11取消照片库并打开相机后,它总是打开照片库.这只发生在iOS 11中.

代码在Xcode 9 Beta 4中编译.

代码如下:

@IBAction func buttonProfilePicPressed(_ sender: UIButton)
{
    let alert = UIAlertController(title: "Choose Image", message: nil, preferredStyle: .actionSheet)
    alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { _ in
        self.openCamera()
    }))

    alert.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { _ in
        self.openGallary()
    }))

    alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))

    self.present(alert, animated: true, completion: nil)
    imgPicker.delegate = self
    self.present(imgPicker, animated: true, completion: nil)
}

func openCamera()
{
    if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.camera))
    {
        imgPicker.sourceType = UIImagePickerControllerSourceType.camera
        imgPicker.allowsEditing = true
        self.present(imgPicker, animated: true, completion: nil)
    }
    else
    {
        let alert  = UIAlertController(title: "Warning", message: "You don't have camera", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
}

func openGallary()
{
    imgPicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
    imgPicker.allowsEditing = true
    self.present(imgPicker, animated: true, completion: nil)
}

Vladimir Pri.. 5

我发现了什么是错的.

MAIN:您必须在呈现UIImagePickerController之前设置sourceType.关于这一点,您可以阅读文档UIImagePickerController.

是的,您可以看到sourceType的文档,但有关文档页面上的sourceType的信息对于iOS 11是错误的或不实际的.

结果:

    首先,您必须配置UIImagePickerController

    其次是介绍它.

在您的情况下,您只需要删除一行:

@IBAction func buttonProfilePicPressed(_ sender: UIButton)
{
    ...
    self.present(imgPicker, animated: true, completion: nil) //REMOVE IT!!!!!111
}

PS检查并使用Xcode 9 GM



1> Vladimir Pri..:

我发现了什么是错的.

MAIN:您必须在呈现UIImagePickerController之前设置sourceType.关于这一点,您可以阅读文档UIImagePickerController.

是的,您可以看到sourceType的文档,但有关文档页面上的sourceType的信息对于iOS 11是错误的或不实际的.

结果:

    首先,您必须配置UIImagePickerController

    其次是介绍它.

在您的情况下,您只需要删除一行:

@IBAction func buttonProfilePicPressed(_ sender: UIButton)
{
    ...
    self.present(imgPicker, animated: true, completion: nil) //REMOVE IT!!!!!111
}

PS检查并使用Xcode 9 GM


推荐阅读
author-avatar
a君bb_307
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有