CameraButton 是 UIButton 的子类,允许您在任意应用程序中以如同设置显示相机选项按钮一样简单的方式包括拍照功能,并更改一些配置
将 CameraButton.swift
文件添加到您的项目中。
Add `import CameraButton.swift` in your file
在 Storyboard 中将类分配给用于显示相机的按钮,
并为该按钮创建一个出口
例如。
@IBOutlet weak var myCameraButton: CameraButton!
将按钮附加到动作等。
@IBAction func myCameraAction(sender: AnyObject) {
myCameraButton.openCameraOptionMenu()
}
在视图控制器中
例如。
let cameraButton: CameraButton = CameraButton(frame: CGRectMake(100, 400, 100, 50))
cameraButton.setTitle("Click Me", forState: UIControlState.Normal)
cameraButton.addTarget(self, action: "myCameraAction:", forControlEvents: UIControlEvents.TouchUpInside) //Attach a action to the button
self.view.addSubview(cameraButton) // add to view as subview
//create the action
@IBAction func myCameraAction(sender: AnyObject) {
myCameraButton.openCameraOptionMenu()
}
func setupCameraButton() {
// required
myCameraButton.targetViewController = self
//optional
myCameraButton.targetImageView = self.myImageView // self.myImageView is the UIImageView to put the selected image
myCameraButton.optinMenuHeaderTitle = "Camera Options"
}
或者如果您想使用代理选项来操作图像
使用视图控制器添加代理选项
class ViewController: UIViewController, CameraButtonDelegate {
...
}
func setupCameraButton() {
// required
myCameraButton.targetViewController = self
//optional
myCameraButton.delegate = self
}
func changeMenuOptionsForCamera() {
//only camera option will be shown
//default optios are .Camera, .PhotoLibrary, .PhotoAlbum, .DeleteExistingImage
//.DeleteExistingImage image will only work when the targetImageView controller contains image
// or cameraButton.imageViewHasImage = true
// if this flag is true, the existing image from the targetImageView would be removed, even though it has not been
// picked using the camera button image picker
myCameraButton.optionMenuList = [ .Camera]
}
代理人
/* the selected image can be manipulated even if the target image view is not specified */
func imagePickerDismissed(imagePicked : Bool, withImage : UIImage?) {
if (imagePicked == true && withImage != nil) {
self.myImageView.image = withImage!
}
}
func targetImageDeleted() {
//whatever action needs to be taken after the image has been deletated
//e.g.
self.myImageView.image = nil
}
if let selectedImage : UIImage = self.myCameraButton.getSelectedImage() {
//do something with the selected image
}
if let selectedImageData : NSData = self.myCameraButton.getSelectedImageAsData(type: .PNG) {
//do something with the selected selectedImageData
}
if let selectedImageDataAsString : String = self.myCameraButton.getImageAsBase64EncodedString(type: .PNG) {
//do something with the selected selectedImageDataAsString
}
//a custom imagepicker which has to be a subclass of UIImagePickerController can be used if necessary
public lazy var imagePicker : UIImagePickerController = UIImagePickerController()
//a required option, generally this is the view controller where the Camera Button is placed
public weak var targetViewController : UIViewController?
//if not provided, a dummy one would be created
public weak var targetImageView : UIImageView?
//a image name that could be placed in targetImageView after the image has been deleted
public var placeHolderImageName : String = ""
//if true, indicates the targetImageView contains a image, and the delete option is shown if not otherwise disabled
public var imageViewHasImage : Bool = false
/**settings for different camera action,
*
* can be disabled by setting show = false
* e.g.
* self.cameraButton.cameraMenuSettings.show = false, the camera option will not be shown in menu
* if not available a message can be shown
* no message would be shown if notAvailableMessage is kept empty
*
*/
public var cameraMenuSettings : MenuSettings = MenuSettings(type : .Camera, name: "Camera", show: true, allowsEditing : false, presentWithAnimation : true, dismissWithAnimation : true, notAvailableMessage : "Unable to find camera in this device")
//settings for photo library action
public var photoLibraryMenuSettings : MenuSettings = MenuSettings(type : .PhotoLibrary, name: "Photo Library", show: true, allowsEditing : false, presentWithAnimation : true, dismissWithAnimation : true, notAvailableMessage : "Unable to find photo library in this device")
//settings for photo album action
public var photoAlbumMenuSettings : MenuSettings = MenuSettings(type : .PhotoAlbum, name: "Photo Album", show: true, allowsEditing : false, presentWithAnimation : true, dismissWithAnimation : true, notAvailableMessage : "Unable to find photo album in this device")
//the allowsEditing, prsentWithAnimation and dismissWithAnimation, notAvailableMessage does not really effect this menu
public var deleteMenuSettings : MenuSettings = MenuSettings(type : .DeleteExistingImage, name: "Delete", show: true, allowsEditing : false, presentWithAnimation : true, dismissWithAnimation : true, notAvailableMessage : "")
//The header of the menu
public var optinMenuHeaderTitle : String = ""
//this option should be used to change the order of availabel options, can also be used to not show an option
public var optionMenuList : Array<MenuOptonTypes> = [
.Camera, .PhotoLibrary, .PhotoAlbum, .DeleteExistingImage
]
//user can choose to show the option menu popover from other places
public weak var showOptionMenuFromView : UIView?
//user can choose to show the option menu popover from bar button item
public weak var showOptionFromBarButtonItem : UIBarButtonItem?
需要 Swift2.0 和至少 iOS 7.0
欢迎拉取请求、补丁和其他反馈。
[Subarna]
CameraButton 在MIT许可证下提供。有关更多信息,请参阅LICENSE文件。