Swift-->从相册(摄像头)选择照片,图片选择

前端之家收集整理的这篇文章主要介绍了Swift-->从相册(摄像头)选择照片,图片选择前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一个APP,选择照片是很常用的功能,作为Swift上路的菜鸟,记录路上的点点滴滴.

效果:(MAC 原生截图,真JB大…我还特意用了小屏幕的iPhone4s…还是很大….)


界面部分不好说,文章不提了….有兴趣的加群讨论!!!

1:处理点击图片,弹出选择对话框

//MARK: 轻触 图片控件
extension ViewController {

    // MARK: 用于弹出选择的对话框界面
    var selectorController: UIAlertController {
        let controller = UIAlertController(title: nil,message: nil,preferredStyle: .ActionSheet)
        controller.addAction(UIAlertAction(title: "取消",style: .Cancel,handler: nil)) // 取消按钮
        controller.addAction(UIAlertAction(title: "拍照选择",style: .Default) { action in
            self.selectorSourceType(.Camera)
        }) // 拍照选择
        controller.addAction(UIAlertAction(title: "相册选择",style: .Default) { action in
            self.selectorSourceType(.PhotoLibrary)
        }) // 相册选择
        return controller
    }

    // MARK: 轻触手势事件的回调
    @IBAction func onTapImageView(sender: UITapGestureRecognizer) {
        presentViewController(selectorController,animated: true,completion: nil)
    }

    func selectorSourceType(type: UIImagePickerControllerSourceType) {
        imagePickerController.sourceType = type
        // 打开图片选择器
        presentViewController(imagePickerController,completion: nil)
    }
}

2:调用系统的图片选择界面

//MARK: 扩展图片选择和结果返回
extension ViewController: UIImagePickerControllerDelegate,UINavigationControllerDelegate {

    // MARK: 图片选择器界面
    var imagePickerController: UIImagePickerController {
        get {
            let imagePicket = UIImagePickerController()
            imagePicket.delegate = self
            imagePicket.sourceType = .PhotoLibrary
            return imagePicket
        }
    }

    // MARK: 当图片选择器选择了一张图片之后回调
    func imagePickerController(picker: UIImagePickerController,didFinishPickingMediaWithInfo info: [String: AnyObject]) {
        dismissViewControllerAnimated(true,completion: nil) // 选中图片,关闭选择器...这里你也可以 picker.dismissViewControllerAnimated 这样调用...但是效果都是一样的...

        imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage // 显示图片
        imageView.contentMode = .ScaleToFill // 缩放显示,便于查看全部的图片
    }

    // MARK: 当点击图片选择器中的取消按钮时回调
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        picker.dismissViewControllerAnimated(true,completion: nil) // 效果一样的...
    }
}

3:ViewController其他代码
此类非常简单,归功于Swift强大的扩展(extension)功能.

class ViewController: UIViewController {

    // 图片控件
    @IBOutlet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view,typically from a nib.
        imageView.userInteractionEnabled = true // 开启控件的交互操作,否则轻触事件无法传递
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

源码: https://github.com/angcyo/ImagePickerDemo

细心的你,可能发现了上图中全TM是英文….嘿嘿!!!

你懂得…


至此: 文章就结束了,如有疑问: QQ群 Android:274306954 Swift:399799363 欢迎您的加入.

猜你在找的Swift相关文章