First, Introduction
Image selection or photo function:
1, select pictures in the album or take photos, are through the Uiimagepickercontroller controller instantiation of an object, and then through the Self.presentviewcontroller method to launch the interface display. But classes that use Presentviewcontroller need to implement Uiimagepickercontrollerdelegate, Uinavigationcontroller two proxies.
2, Uiimagepickercontroller can use the Issourcetypeavailable method to determine whether the device supports camera/Photo Library/album function. If supported, you can set the display type of the picture controller through the SourceType property. Types are divided into 3 categories: photolibrary (photo gallery), camera (cameras), Savedphotoalbum (Photo album)
3. Implement the Agreement
Second, examples
1. Use album Selection steps:
- Determine if the image Library or album feature you want to use is supported
- Initialize the picture Controller object
- Specify the proxy for the picture controller object
- Specifies the type of the picture controller, provided that the Mobilecoreservices.framework framework must be imported first
- Pop-up Display picture controller
- Implementing a Picture Control Agent method
func chooseimagefromalbum () {//determine if the picture library you want to use is supported ifuiimagepickercontroller.issourcetypeavailable (. photolibrary) {//Initialize the picture controllerLet picker =Uiimagepickercontroller ()//Set up proxyPicker.Delegate= Self//Set Media typePicker.mediatypes = [Kuttypeimage asString,kuttypevideo asString]//setting allows editingPicker.allowsediting =true //Specify the picture controller typePicker.sourcetype =. Photolibrary//eject controller, display interfaceSelf.present (picker, animated:true, Completion:nil)} Else{let alert= Uialertview.init (title:"Read album Error!", Message:nil,Delegate: Nil, Cancelbuttontitle:"Determine") Alert.show ()}}//Implementing a Picture Controller proxy methodFunc Imagepickercontroller (_ Picker:uiimagepickercontroller, didfinishpickingmediawithinfo info: [String:any]) { //View Info ObjectPrint (info)//get the selected artworkLet Originimage = Info[uiimagepickercontrolleroriginalimage] as!UIImage//assignment, picture view display PictureSelf.pickerView.image =Originimage//Picture Controller ExitPicker.dismiss (Animated:true, Completion:nil)} //canceling the picture controller agentFunc Imagepickercontrollerdidcancel (_ Picker:uiimagepickercontroller) {//Picture Controller ExitPicker.dismiss (Animated:true, Completion:nil)}
2. Photo steps:
- Determine if the camera support feature is supported
- Initialize the picture Controller object (you can set whether to allow editing)
- Specify the proxy for the picture controller object
- Specify the type of the picture controller
- Pop-up Display picture controller
func Takephotofromcamera () {//determine if the camera is supported ifuiimagepickercontroller.issourcetypeavailable (. photolibrary) {//Initialize the picture controllerLet picker =Uiimagepickercontroller ()//Set up proxyPicker.Delegate= Self//Set Media typePicker.mediatypes = [Kuttypeimage asString,kuttypevideo asString]//Set SourcePicker.sourcetype =Uiimagepickercontrollersourcetype.camera//Set Lens front: front-facing camera Rear: rear -facing camera ifuiimagepickercontroller.iscameradeviceavailable (uiimagepickercontrollercameradevice.front) {picker.ca Meradevice=Uiimagepickercontrollercameradevice.front}//set Flash (on: off: OFF, Auto: Automatic)Picker.cameraflashmode =Uiimagepickercontrollercameraflashmode.on//Allow EditingPicker.allowsediting =true //turn on the cameraSelf.present (picker, animated:true, Completion:nil)} Else{let alert= Uialertview.init (title:"can't find the camera!", Message:nil,Delegate: Nil, Cancelbuttontitle:"Determine") Alert.show ()}}
Swift3.0: Photo Selection