多媒体应用-swift
生活随笔
收集整理的這篇文章主要介紹了
多媒体应用-swift
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
照片選擇主要是通過UIImagePickerController控制器實例化一個對象,然后通過self.PresentViewController方法推出界面顯示。需要實現代理UIImagePickerControllerDelegate,UINavigationControllerDelegate。
通過isSourceTypeAvailable方法判斷設置是否支持照相機、圖片庫、相冊功能。
使用相冊功能主要以下一個步驟:
1)判斷是否支持要使用的圖片庫或相機功能;
2)初始化圖片控制器對象;
3)指定圖片控制器對象的代理;
4)指定圖片控制器類型;
5)彈出顯示圖片控制器;
6)實現圖片控制器代理方法。
// MARK: - 選擇照片/*----- 選擇照片 ------*/@IBAction func addImageButtonClick(){let actionSheet = UIActionSheet(title: "請選擇", delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: nil, otherButtonTitles: "從相冊選","拍照")actionSheet.showInView(self.view)} // MARK: - UIActionSheetDelegate func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int){if buttonIndex != actionSheet.cancelButtonIndex{if buttonIndex == 1 //從相冊選 {self.fromAlbum()}else if buttonIndex == 2 //拍照 {self.fromPhotograph()}}} // MARK: - 選取相冊 func fromAlbum(){//判斷設置是否支持圖片庫if UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary){//初始化圖片控制器let picker = UIImagePickerController()//設置代理picker.delegate = self//設置媒體類型picker.mediaTypes = [kUTTypeImage as String,kUTTypeVideo as String]//設置允許編輯picker.allowsEditing = true//指定圖片控制器類型picker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary//彈出控制器,顯示界面self.presentViewController(picker, animated: true, completion: { () -> Void in})}else{let aler = UIAlertView(title: "讀取相冊錯誤!", message: nil, delegate: nil, cancelButtonTitle: "確定")aler.show()}} // MARK: - 拍照 func fromPhotograph(){if UIImagePickerController.isSourceTypeAvailable(.Camera){//創建圖片控制器let picker = UIImagePickerController()//設置代理picker.delegate = self//設置來源picker.sourceType = UIImagePickerControllerSourceType.Camera//設置鏡頭if UIImagePickerController.isCameraDeviceAvailable(UIImagePickerControllerCameraDevice.Front){picker.cameraDevice = UIImagePickerControllerCameraDevice.Front}//設置閃光燈picker.cameraFlashMode = UIImagePickerControllerCameraFlashMode.On//允許編輯picker.allowsEditing = true;//打開相機self.presentViewController(picker, animated: true, completion: { () -> Void in})}else{let aler = UIAlertView(title: "找不到相機!", message: nil, delegate: nil, cancelButtonTitle: "確定")aler.show()}} // MARK: - UIImagePickerControllerDelegate//選擇圖片成功之后代理 func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {//查看info對象 print(info)//獲取選擇的原圖let image = info[UIImagePickerControllerOriginalImage] as! UIImage//賦值,圖片視圖顯示圖片picView.image = image//圖片控制器退出picker.dismissViewControllerAnimated(true, completion: { () -> Void in})}//取消圖片控制器代理 func imagePickerControllerDidCancel(picker: UIImagePickerController){//圖片控制器退出picker.dismissViewControllerAnimated(true, completion: { () -> Void in})}?可以通過引入MobileCoreServices.framework,來設置mediaTypes屬性設置媒體的類型。
?
轉載于:https://www.cnblogs.com/fengmin/p/5713749.html
總結
以上是生活随笔為你收集整理的多媒体应用-swift的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: css样式之 direction
- 下一篇: TableLayoutPanel