您的位置:首页 > 移动开发 > Swift

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

2015-10-28 14:47 375 查看
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()]

                //改navigationBar的button字体色

                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()]

                    //改navigationBar的button字体色

                    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)

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios swift 相机和相册