打開相冊

//判斷camera是否可用
UIImagePickerController.isCameraDeviceAvailable(.Front)
//判斷某個數(shù)據(jù)方式是否可用
UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType)
// 裁剪圖片
Vc.allowsEditing = true
// 打開相冊
func selectImage(){

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

代理方法,準(zhǔn)守的協(xié)議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{
        
        // 如果當(dāng)前圖片的寬度 小于程序員指定的寬度
        if self.size.width < width {
            return self
        }
        // 獲取圖片的最終高度
        let height = (width*self.size.height)/self.size.width
        // 設(shè)置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 關(guān)閉上下文
        UIGraphicsEndImageContext()
        return result
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容