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)
    }
    







你可能感兴趣的:(Swift,小栏)