IOS SWIFT alte形式获取手机的相册和相机

import UIKit


class ViewController: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate{

    

    let imagePickerController: UIImagePickerController = UIImagePickerController()

    var isFullScreen: Bool = false

    var imageData=NSData()

    var img = UIImage()


    @IBOutlet weak var image: UIImageView!

    

    @IBAction func btnClicked(sender: UIButton) {

        // 设置代理

        self.imagePickerController.delegate = self

        // 设置是否可以管理已经存在的图片或者视频

        self.imagePickerController.allowsEditing = true

        // 判断是否支持相机

        if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)){

            let alertController: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)

            let cameraAction: UIAlertAction = UIAlertAction(title: "拍照换头像", style: .Default) { (action: UIAlertAction!) -> Void in

                // 设置类型

                self.imagePickerController.sourceType = UIImagePickerControllerSourceType.Camera

                self.presentViewController(self.imagePickerController, animated: true, completion: nil)

            }

            alertController.addAction(cameraAction)

            let photoLibraryAction: UIAlertAction = UIAlertAction(title: "从相册选择换头像", style: .Default) { (action: UIAlertAction!) -> Void in

                // 设置类型

                self.imagePickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary

                //navigationBar背景色

                self.imagePickerController.navigationBar.barTintColor = UIColor(red: 171/255, green: 202/255, blue: 41/255, alpha: 1.0)

                //navigationBar标题色

                self.imagePickerController.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]

                //navigationBarbutton字体色

                self.imagePickerController.navigationBar.tintColor = UIColor.whiteColor()

                self.presentViewController(self.imagePickerController, animated: true, completion: nil)

            }

            alertController.addAction(photoLibraryAction)

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

            alertController.addAction(cancelAction)

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

            

            }else{

            let alertController: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)

            let photoLibraryAction: UIAlertAction = UIAlertAction(title: "从相册选择换头像", style: .Default) { (action: UIAlertAction!) -> Void in

                    // 设置类型

                    self.imagePickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary

                    //navigationBar背景色

                    self.imagePickerController.navigationBar.barTintColor = UIColor(red: 171/255, green: 202/255, blue: 41/255, alpha: 1.0)

                    //navigationBar标题色

                    self.imagePickerController.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]

                    //navigationBarbutton字体色

                    self.imagePickerController.navigationBar.tintColor = UIColor.whiteColor()

                    self.presentViewController(self.imagePickerController, animated: true, completion: nil)

            }

            alertController.addAction(photoLibraryAction)

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

            alertController.addAction(cancelAction)

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

        }

      }

    

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

        picker.dismissViewControllerAnimated(true, completion: nil)

        img = info[UIImagePickerControllerEditedImage] as! UIImage

        image.image = img

        imageData=UIImageJPEGRepresentation(img, 1)!

        //保存文件

        var path:Array = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomainMask.UserDomainMask,true)

        let FilePath=path[0].stringByAppendingString("/HomeImage.png")

        imageData.writeToFile(FilePath, atomically: true)

        picker.dismissViewControllerAnimated(true, completion: nil)

    }

    // 当用户取消时,调用该方法

    func imagePickerControllerDidCancel(picker: UIImagePickerController) {

        self.dismissViewControllerAnimated(true, completion: nil)

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值