protocol TMMutableImageViewDelegate {
func imageSelected(editedImage: UIImage, originalImage: UIImage)
}
class TMMutableImageView: UIImageView, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
private var viewController: UIViewController!
var delegate: TMMutableImageViewDelegate?
func setUp(viewController: UIViewController) {
self.viewController = viewController
self.userInteractionEnabled = true
let tapGestureRecognizer = UITapGestureRecognizer()
tapGestureRecognizer.addTarget(self, action: "handleImageViewTap:")
self.addGestureRecognizer(tapGestureRecognizer)
}
func handleImageViewTap(gestureRecognizer: UITapGestureRecognizer) {
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
let captureAction = UIAlertAction(title: "Capture", style: UIAlertActionStyle.Default) { (action: UIAlertAction!) -> Void in
self.presentPhotoPickerOrCamera(UIImagePickerControllerSourceType.Camera)
}
let chooseAction = UIAlertAction(title: "Choose", style: UIAlertActionStyle.Default) { (action: UIAlertAction!) -> Void in
self.presentPhotoPickerOrCamera(UIImagePickerControllerSourceType.PhotoLibrary)
}
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
alertController.addAction(captureAction)
alertController.addAction(chooseAction)
alertController.addAction(cancelAction)
self.viewController.presentViewController(alertController, animated: true, completion: nil)
}
func presentPhotoPickerOrCamera(sourceType: UIImagePickerControllerSourceType) {
let imagePickerController = UIImagePickerController()
// WARNING: crash on simulator if sourceType is .Camera
imagePickerController.sourceType = sourceType
imagePickerController.delegate = self
imagePickerController.allowsEditing = true
self.viewController.presentViewController(imagePickerController, animated: true, completion: nil)
}
// MARK: UIImagePickerControllerDelegate
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let editedImage = info[UIImagePickerControllerEditedImage] as! UIImage
let originalImage = info[UIImagePickerControllerOriginalImage] as! UIImage
picker.dismissViewControllerAnimated(true, completion: nil)
self.delegate?.imageSelected(editedImage, originalImage: originalImage)
self.image = editedImage
}
}
Customized UIImageView that allows choosing photo from library or camera
最新推荐文章于 2023-08-03 02:16:19 发布