swift2.0 Uiimagepickercontroller Photo Album video

Source: Internet
Author: User

System ios9.1
Language swift2.0

The most common feature in the app is multimedia selection, first we Storyboard create a button to trigger the selection event

@IBAction func selectImageAction(sender: AnyObject) {}

It usually pops up with a actionsheet with photos, albums, videos and cancellations. IOS 8 Actionsheet and Alertview are unified with the Uialertcontroller method call, 8.3 before the Actionsheet have a separate method was later discarded. First we want to join Actionsheet related delegate as follows

class ViewController: UIViewController ,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate {}
@IBAction func selectimageaction (sender:anyobject) {Let Actionsheetcontroller:uialertcontroller = Uialertcontrolle R (Title:"Please select", Message:nil, Preferredstyle:. Actionsheet)//Cancel button Let Cancelaction:uialertaction = Uialertaction (title:"Cancel", Style:. Cancel) {action-a VoidIn//just dismiss the action sheet} actionsheetcontroller. Addaction (cancelaction)//Photo Let takepictureaction:uialertaction = uialertaction (title:"Take pictures", style:. Default) {action, Voidin [Self. Initwithimagepickview ("Photo")]} Actionsheetcontroller. Addaction (Takepictureaction)//album Select Let Choosepictureaction:uialertaction = Uialertaction (title: "album", Style: in [self  "album")]} actionsheetcontroller" camera ", Style: in [self  "Camera")]} actionsheetcontroller.presentviewcontroller (ActionSheetController, Animated:true, Completion:nil)}            

So we want the actionsheet created to complete
Look closely at the code above and you will find [self. Initwithimagepickview ("Take photos")]
This function, I choose imagepicker different parameters through this function
First create a var imagepicker:uiimagepickercontroller! Object
Then implement the relevant methods

Func Initwithimagepickview (type:nsstring) {self. Imagepicker = Uiimagepickercontroller () Self. imagepicker. Delegate = Self; Self. imagepicker. Allowsediting = True; Switch type{case"Take pictures": Self. imagepicker. sourcetype =. CameraBreak case"Album": Self. imagepicker. sourcetype =break case  "video": Self .imagepicker.sourcetype = .imagepicker.videomaximumduration = Span class= "Hljs-number" >60 * 3 self.imagePicker.videoquality = .imagepicker.mediaTypes = [String (Kuttypemovie)] break default:print ( "error")} Presentviewcontroller (self< Span class= "Hljs-preprocessor" >.imagepicker, Animated:true, Completion:nil)}      

Swift can support strings, you can also try other types to see! Some methods are basically the same as Object C

The following implementation of the Imagepick delegate method is possible
As follows
Yes, if you want to introduce the recording video function, don't forget to add
Mobilecoreservices.framework Library and import mobilecoreservices header files

Func Imagepickercontroller (Picker:uiimagepickercontroller, didfinishpickingmediawithinfo info: [String:anyobject]) {

        Let mediatype = Info[uiimagepickercontrollermediatype]as! StringLet Compareresult = cfstringcompare (mediatypeAs nsstring!, Kuttypemovie, cfstringcompareflags.comparecaseinsensitive)//interpretation is video or picture if compareresult = Cfcomparisonresult.compareequalto {let Moviepath = Info[uiimagepickercontrollermediaurl ] as? Nsurl //Get path let moviepathstring = moviepath!. RelativePath if uivideoatpathiscompatiblewithsavedphotosalbum (moviePathString!) {Uisavevideoatpathtosavedphotosalbum (moviepathstring!, nil, nil, nil)} print ( "video")} else {print ( "picture") let image = info[ Uiimagepickercontrolleroriginalimage] as? UIImage self.imageView.image = image; } imagepicker.dismissviewcontrolleranimated (true, Completion:nil)}   

Such a multimedia selection function is basically realized!

Demo

swift2.0 Uiimagepickercontroller Photo Album video

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.