打开相册

//判断camera是否可用
UIImagePickerController.isCameraDeviceAvailable(.Front)
//判断某个数据方式是否可用
UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType)
// 裁剪图片
Vc.allowsEditing = true
// 打开相册
func selectImage(){

    let pickerVc = UIImagePickerController()
    // 设置代理
    pickerVc.delegate = self
    self.presentViewController(pickerVc, animated: true, completion: nil)
    
}

代理方法,准守的协议UIImagePickerControllerDelegate,UINavigationControllerDelegate

func imagePickerController(picker:                 UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
        composePictureView.addImages(image.getImageScale(600))
        picker.dismissViewControllerAnimated(true, completion: nil)
    }

图片压缩方法

// 压缩图片
    /*
    指定宽度来压缩
    宽 1200    指定宽度  600
    高 600              x
    */
    func getImageScale(width: CGFloat) -> UIImage{
        
        // 如果当前图片的宽度 小于程序员指定的宽度
        if self.size.width < width {
            return self
        }
        // 获取图片的最终高度
        let height = (width*self.size.height)/self.size.width
        // 设置rect
        let rect = CGRect(x: 0, y: 0, width: width, height: height)
        // 通过上下文
        // 01 开启上下文
        UIGraphicsBeginImageContext(rect.size)
        // 02 将图片渲染到上下文中
        self.drawInRect(rect)
        // 03 从上下文获取image
        let result = UIGraphicsGetImageFromCurrentImageContext()
        // 04 关闭上下文
        UIGraphicsEndImageContext()
        return result
    }

你可能感兴趣的:(打开相册)