Swift3.0: Photo Selection

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.