ios 图库的使用

UIImagePickerControllerDelegate ,UINavigationControllerDelegate

 func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int){
        PickerController = UIImagePickerController()
        if buttonIndex == 0{//图库
            PickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary

        }else if buttonIndex == 2{//相机  取消为1
            if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) == false{
               return
            }
            PickerController.sourceType = UIImagePickerControllerSourceType.Camera

        }
        if buttonIndex==0||buttonIndex == 2{
            PickerController.delegate = self
            PickerController.allowsEditing = true
            //PickerController.mediaTypes = [kUTTypeMovie!]//需要头文件支持 视频文件
            //PickerController.videoQuality = UIImagePickerControllerQualityType.Type640x480
            self.presentViewController(PickerController, animated: true) { () -> Void in
                //println("页面跳转成功回调")
            }
        }

    }

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]){
        //println("选择")
       // println("视频文件地址\(info[UIImagePickerControllerMediaURL])")
        println("图片文件\(info[UIImagePickerControllerOriginalImage])")
//编辑后的image
       let image = info[UIImagePickerControllerEditedImage] as! UIImage
        // info[UIImagePickerControllerReferenceURL]  等等信息

        //var url = info[UIImagePickerControllerMediaURL] as! NSURL
       // println("url = \(url.URLString)")

       // var avAsset = AVURLAsset(URL: url, options: nil)
       // println("时间\(avAsset.duration.value)  : \(avAsset.duration.timescale)")
        var image = info[UIImagePickerControllerOriginalImage] as! UIImage
        //self.presentViewController(upload, animated: true, completion: nil)
        //self.navigationController?.pushViewController(upload, animated: true)
        PickerController.dismissViewControllerAnimated(true, completion: { () -> Void in

        })

    }

// 保存到本地

UIVideoAtPathIsCompatibleWithSavedPhotosAlbum //拍摄的视频保存到相册

 UISaveVideoAtPathToSavedPhotosAlbum(url.path, self, nil, nil)

UIImageWriteToSavedPhotosAlbum 拍摄的图片保存到本地

 if picker.sourceType == UIImagePickerControllerSourceType.Camera{
        let saveBool = UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(url.path)
            if saveBool == true{
               UISaveVideoAtPathToSavedPhotosAlbum(url.path, self, nil, nil)
            }
 }
 ```
博客地址:http://chenzhao.date/2016/09/12/ios%E5%9B%BE%E5%BA%93%E4%BD%BF%E7%94%A8.html

你可能感兴趣的:(ios 图库的使用)