Skip to content

Latest commit

 

History

History
95 lines (79 loc) · 4.28 KB

README.md

File metadata and controls

95 lines (79 loc) · 4.28 KB

BRNImagePickerSheet

About

BRNImagePickerSheet is a duplicate of that shiny new custom action sheet seen in iOS8's iMessage that Apple didn't make part of UIKit. It's the first project I've written in Swift. It works well but I might have coded something the Objective-C kind of way. Don't hesitate to open an issue or pull request if you spotted something. And no, BRNImagePickerSheet does not have the glitches Apple's image picker has :)

demo

Twitter: @larcus94 License Carthage compatible

Author

I'm Laurin Brandner, I'm on Twitter.

Usage

BRNImagePickerSheet's API is similar to the one of UIActionSheet so you should get along with it just well.

Example

func presentImagePickerSheet(gestureRecognizer: UITapGestureRecognizer) {
    let authorization = PHPhotoLibrary.authorizationStatus()

    if authorization == .NotDetermined {
        PHPhotoLibrary.requestAuthorization({ (status) -> Void in
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.presentImagePickerSheet(gestureRecognizer)
                })
            })

            return
        }

        if authorization == .Authorized {
            var sheet = BRNImagePickerSheet()
            sheet.numberOfButtons = 3
            sheet.delegate = self
            sheet.showInView(self.view)
        }
        else {
            let alertView = UIAlertView(title: NSLocalizedString("An error occurred", comment: "An error occurred"), message: NSLocalizedString("BRNImagePickerSheet needs access to the camera roll", comment: "BRNImagePickerSheet needs access to the camera roll"), delegate: nil, cancelButtonTitle: NSLocalizedString("OK", comment: "OK"))
                alertView.show()
        }
    }
}
func imagePickerSheet(imagePickerSheet: BRNImagePickerSheet, titleForButtonAtIndex buttonIndex: Int) -> String {
    let photosSelected = (imagePickerSheet.selectedPhotos.count > 0)

    if (buttonIndex == 0) {
        if photosSelected {
            return NSLocalizedString("Add comment", comment: "Add comment")
        }
        else {
            return NSLocalizedString("Take Photo Or Video", comment: "Take Photo Or Video")
        }
    }
    else {
        if photosSelected {
            return NSString.localizedStringWithFormat(NSLocalizedString("BRNImagePickerSheet.button1.Send %lu Photo", comment: "The secondary title of the image picker sheet to send the photos"), imagePickerSheet.selectedPhotos.count)
        }
        else {
            return NSLocalizedString("Photo Library", comment: "Photo Library")
        }
    }
}

func imagePickerSheet(imagePickerSheet: BRNImagePickerSheet, willDismissWithButtonIndex buttonIndex: Int) {
    if buttonIndex != imagePickerSheet.cancelButtonIndex {
        if imagePickerSheet.selectedPhotos.count > 0 {
                println(imagePickerSheet.selectedPhotos)
        }
        else {
            let controller = UIImagePickerController()
            controller.delegate = self
            controller.sourceType = (buttonIndex == 2) ? .PhotoLibrary : .Camera
            self.presentViewController(controller, animated: true, completion: nil)
            }
        }
    }
}

BRNImagePickerSheet uses a delegate method, similar to UITableView's dataSource, to get the title of a button. In conjunction with stringsdict, this allows for easy translation of various plural forms.

Installation

If you don’t use Carthage (although you really should) you can simply drag the files into your project.

Requirements

BRNImagePickerSheet is written in Swift and links agains Photos.framework. It therefore requires iOS 8 or later.

License

BRNImagePickerSheet is licensed under the MIT License.