IOS9, Swift Judge camera, album permissions, select Picture as Avatar

Source: Internet
Author: User

After iOS7 to open the mobile phone camera or album will need permission, in the IOS9 update the album related API calls

Start with a new Swift project, put a button in SB and drag a click event in the Viewcontroller

ok! button and event settings, we will introduce the library to use, to determine the camera permissions, need to introduce avfoundation.framework, search and add

import avfoundation in Viewcontroller

and follow the following several agents Uiimagepickercontrollerdelegate,uiactionsheetdelegate,uinavigationcontrollerdelegate

Declare the variables we need

var img:uiimageview!
var sheet:uialertcontroller!

var sourcetype = uiimagepickercontrollersourcetype.photolibrary// assigns an initial value type to the sourcetype to prevent the call from collapsing when no assignment occurs

In the viewdidload:

Override Func Viewdidload () {
Super.viewdidload ()
img = Uiimageview (Frame:cgrectmake (20, 120, 100, 100))
Self.view.addSubview (IMG)

}

Since we choose the album or open the camera after the image editing operation is the same, so we put this piece of code into the Open method inside

Open Gallery or Camera
Func open () {

Let Imagepickercontroller:uiimagepickercontroller = Uiimagepickercontroller ()
Imagepickercontroller.delegate = Self
Imagepickercontroller.allowsediting = True//true to take pictures, select Finish to enter picture editing mode
Imagepickercontroller.sourcetype = SourceType
Self.presentviewcontroller (Imagepickercontroller, Animated:true, completion:{

})

}

We then encapsulate the code that determines the album permissions and camera permissions into the respective methods to invoke

/**
Judging camera permissions

-Returns: Has permission to return true, no permission to return false
*/
Func camerapermissions ()-bool{

Let Authstatus:avauthorizationstatus = Avcapturedevice.authorizationstatusformediatype (AVMediaTypeVideo)

if (authstatus = = Avauthorizationstatus.denied | | authstatus = = avauthorizationstatus.restricted) {
return False
}else {
return True
}

}
(the camera permission is basically the same as OC, only the method call method changes)

   /**
     determine album permissions
    &NBSP
    -Returns: Have permission to return ture, no permission to return False
     */
   &NBSP
    func photolibrarypermissions (), Bool {
        
        Let library:phauthorizationstatus = Phphotolibrary.authorizationstatus ()
        if (Library = = phauthorizationstatus.denied | | Library = = phauthorizationstatus.restricted) {
             return False
       }else {
             return True
       }
   }

(album permissions to judge here before iOS9 are used Assetslibrary Library, after iOS9 reference is photos library, although still can call assetslibrary Library to judge, but there will be warning Photos Library Reference, Import Photos on the line)

The next step is to write the code in the button event that was dragged ahead:


@IBAction func Picker (sender:anyobject) {

Determine if the settings support picture libraries and cameras
if (uiimagepickercontroller.issourcetypeavailable (Uiimagepickercontrollersourcetype.camera) && Uiimagepickercontroller.issourcetypeavailable (uiimagepickercontrollersourcetype.photolibrary)) {

Sheet = Uialertcontroller (title:nil, message: "Choose get Avatar Mode", Preferredstyle:. Actionsheet)

Cancel
Let cancelaction = uialertaction (title: "Cancel", Style:.) Cancel, Handler: {(action) in
Print ("Cancel")
})
Sheet.addaction (cancelaction)


Album
Let okaction = uialertaction (title: "Album", Style:.) Default, Handler: {(action) in
if (self. Photolibrarypermissions () = = True) {
Self.sourcetype = Uiimagepickercontrollersourcetype.photolibrary
Self.open ()
}else{
Pop-up Prompt box
Self.sheet = Uialertcontroller (title:nil, message: "Please open album Permissions in Settings", Preferredstyle:. Alert)

Let tempaction = uialertaction (title: "OK", Style:.) Cancel) {(action) in
Print ("Cancel")

}
Self.sheet.addAction (tempaction)
Self.presentviewcontroller (Self.sheet, Animated:true, Completion:nil)
}
})
Sheet.addaction (okaction)


Camera
Let destroyaction = uialertaction (title: "Webcam", Style:.) Default, Handler: {(action) in
if (self.camerapermissions () = = True) {
Self.sourcetype = Uiimagepickercontrollersourcetype.camera
Self.open ()
}else {
Pop-up Prompt box
Self.sheet = Uialertcontroller (title:nil, message: "Please turn on camera permissions in Settings", Preferredstyle:. Alert)

Let tempaction = uialertaction (title: "OK", Style:.) Cancel) {(action) in
}
Self.sheet.addAction (tempaction)
Self.presentviewcontroller (Self.sheet, Animated:true, Completion:nil)
}
})
Sheet.addaction (destroyaction)

}

Self.presentviewcontroller (Self.sheet, Animated:true, Completion:nil)
}


At last


Cancel a picture selection operation
Func Imagepickercontrollerdidcancel (Picker:uiimagepickercontroller)
{
Self.dismissviewcontrolleranimated (True, Completion:nil)
}



Finish selecting picture Actions
Func Imagepickercontroller (Picker:uiimagepickercontroller, Didfinishpickingimage image:uiimage!, EditingInfo: [ nsobject:anyobject]!) {
Img.image = Image
Self.dismissviewcontrolleranimated (True, Completion:nil)

}

Note: The demo can only be run on the real machine because the camera is judged by the previous

Attach Demo Link: Click to open link

IOS9, Swift Judge camera, album permissions, select Picture as Avatar

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.