After iOS7 to open mobile phone camera or album words need permissions, in the IOS9 is updated 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, the need to introduce avfoundation.framework, search and add
Import Avfoundation in Viewcontroller
and follow the following several agent Uiimagepickercontrollerdelegate,uiactionsheetdelegate,uinavigationcontrollerdelegate
Declaring the variables we need
var img:uiimageview!
var sheet:uialertcontroller!
var sourcetype = uiimagepickercontrollersourcetype.photolibrary//Assign sourcetype an initial value type to prevent the call from not assigning a crash
In the Viewdidload:
Override Func Viewdidload () {
Super.viewdidload ()
img = Uiimageview (Frame:cgrectmake (20, 120, 100, 100))
Self.view.addSubview (IMG)
}
Since we chose the photo album or opened the camera to edit the image after the operation is the same, so we have this code encapsulated in the open method inside
Open a gallery or camera
Func open () {
Let Imagepickercontroller:uiimagepickercontroller = Uiimagepickercontroller ()
Imagepickercontroller.delegate = Self
Imagepickercontroller.allowsediting = True//true for taking photos, choosing to go into picture editing mode
Imagepickercontroller.sourcetype = SourceType
Self.presentviewcontroller (Imagepickercontroller, Animated:true, completion:{
})
}
We then encapsulate the code that determines the permissions of the album and the camera to the respective methods of the call
/**
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
}
}
(Camera permissions and OC are basically consistent, but the method call method changes only)
/**
Judging album permissions
-Returns: Have permission to return ture, no permission to return False
*/
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 in the iOS9 before the Assetslibrary library, in iOS9 after the reference is photos library, although still can call the Assetslibrary library to judge, but there will be warnings photos reference to the library, import Photos on the line)
The next step is to write the code in the button event that you dragged out earlier:
@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 to get Avatar Way", 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 Cue box
Self.sheet = Uialertcontroller (title:nil, Message: "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: "Camera", style:. Default, Handler: {(action) in
if (self.camerapermissions () = = True) {
Self.sourcetype = Uiimagepickercontrollersourcetype.camera
Self.open ()
}else {
Pop-up Cue box
Self.sheet = Uialertcontroller (title:nil, message: "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 Picture selection operation
Func Imagepickercontrollerdidcancel (Picker:uiimagepickercontroller)
{
Self.dismissviewcontrolleranimated (True, Completion:nil)
}
Select the picture action to finish
Func Imagepickercontroller (Picker:uiimagepickercontroller, Didfinishpickingimage image:uiimage!, EditingInfo: [ nsobject:anyobject]!) {
Img.image = Image
Self.dismissviewcontrolleranimated (True, Completion:nil)
}
Note: The camera is judged by the front, and the demo can only run on the real machine.
Attach Demo Link: Click to open link