Swift 调用系统相机 (自己封装的小Demo)


首先 初始化根控制器 在navigationController 建立左button 并且  添加事件
let rightButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "rightAction")
 self.navigationItem.rightBarButtonItem = rightButton

///button 事件

 func rightAction()
    {
        print("...")
        
        let alertAction = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
        
        alertAction.addAction(UIAlertAction(title: "相机", style: UIAlertActionStyle.Default, handler: { (alertAcftion) -> Void in
            
            print("相机")
            
            if (UIImagePickerController.isSourceTypeAvailable(.Camera)) {
                
                let controller = UIImagePickerController()
                
                //选择类型
                controller.sourceType = UIImagePickerControllerSourceType.Camera
                
                //照片类型
                controller.mediaTypes = [kUTTypeImage as String]
                
                //相机代理方法
                controller.delegate = self
                
                //推出相机
                self.presentViewController(controller, animated: true, completion: { () -> Void in
                    
                    print("Picker View Controller is presented")
                })
            
                
            } else {
                
                let alertView = UIAlertController(title: "Error", message: "您没有摄像头", preferredStyle: UIAlertControllerStyle.Alert)
                
                alertView.addAction(UIAlertAction(title: "确定", style: UIAlertActionStyle.Cancel, handler: { (alertView) -> Void in
                    
                    print("cancel。。。")
                }))
               

                //UIAlertController必须为推出视图
                self.presentViewController(alertView, animated: true, completion: nil)
                
            }
           
        }))
        
        alertAction.addAction(UIAlertAction(title: "相册", style: UIAlertActionStyle.Default, handler: { (alertAction) -> Void in
            
            print("相册")
            
            if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary)) {
                
                let controller = UIImagePickerController()
                
                controller.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
                
                controller.mediaTypes = [kUTTypeImage as String]
                
                controller.delegate = self
                
                self .presentViewController(controller, animated: true, completion: { () -> Void in
                    
                    print("Picker View Controller is presented")
                })
      
            }
            
        }))
        
        
        alertAction.addAction(UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: { (UIAlertAction) -> Void in
            print("取消")
            
        }))
        
        self.presentViewController(alertAction, animated: true, completion: nil)

    }

//UIImagePickerControllerDelegate 


    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
        
        picker.dismissViewControllerAnimated(true, completion: nil)
        var image: UIImage!
        // 判断,图片是否允许修改
        if(picker.allowsEditing){
            //裁剪后图片
            image = editingInfo![UIImagePickerControllerEditedImage] as! UIImage
        }else{
            //原始图片
            image = editingInfo![UIImagePickerControllerOriginalImage]as! UIImage
        }
        
        // 保存图片至本地
        self.saveImage(image, newSize: CGSize(width: 256, height: 256), percent: 0.5, imageName: "currentImage.png")
        
        let fullPath: String = NSHomeDirectory().stringByAppendingString("Documents").stringByAppendingString("currentImage.png")
        
        print("fullPath=\(fullPath)")
        
        let savedImage: UIImage = UIImage(contentsOfFile: fullPath)!

        //在这里调用网络通讯方法,上传头像至服务器...
   
    }
    
    //保存图片  至本地
    func saveImage(currentImage: UIImage, newSize: CGSize, percent: CGFloat, imageName: String)
    {
        //压缩图片尺寸
        UIGraphicsBeginImageContext(newSize)
        
        currentImage.drawInRect(CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
        
        let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
       
        //高保真压缩图片质量
        //UIImageJPEGRepresentation此方法可将图片压缩,但是图片质量基本不变,第二个参数即图片质量参数。
        
        let imageData: NSData = UIImageJPEGRepresentation(newImage, percent)!
       
        // 获取沙盒目录,这里将图片放在沙盒的documents文件夹中
        let fullPath: String = NSHomeDirectory().stringByAppendingString("Documents").stringByAppendingString(imageName)
        
        // 将图片写入文件
        imageData.writeToFile(fullPath, atomically: false)
    }
    







  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值