Skip to content

A duplicate of that shiny new custom action sheet seen in iOS8's iMessage

License

Notifications You must be signed in to change notification settings

grimabe/BRNImagePickerSheet

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 

Repository files navigation

ImagePickerSheet

About

ImagePickerSheet 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, ImagePickerSheet 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

ImagePickerSheet'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 = ImagePickerSheet()
            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("ImagePickerSheet needs access to the camera roll", comment: "ImagePickerSheet needs access to the camera roll"), delegate: nil, cancelButtonTitle: NSLocalizedString("OK", comment: "OK"))
                alertView.show()
        }
    }
}
func imagePickerSheet(imagePickerSheet: ImagePickerSheet, 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("ImagePickerSheet.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: ImagePickerSheet, 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)
            }
        }
    }
}

ImagePickerSheet 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

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

License

ImagePickerSheet is licensed under the MIT License.

About

A duplicate of that shiny new custom action sheet seen in iOS8's iMessage

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 98.5%
  • C++ 1.5%