[英]SWIFT | photo gallery not opening
我創建了一個應用程序菜單,用戶可以在其中單擊照片,並且圖片顯示在alertView中,並且在其中添加了2個操作->其中一個操作是“取消”,其他操作是“編輯圖像”,應打開圖庫以選擇其他功能圖片。 但是,當我單擊編輯按鈕(添加了代碼)時,它什么也不起作用,其作用與消除警報視圖相同。 ALERTVIEW的代碼是
@objc func taxImageTApped(_snder:UITapGestureRecognizer) {
print("TaxImage")
let alertView = UIAlertController(title: "Edit Tax Image", message: "", preferredStyle: UIAlertController.Style.alert)
let image = #imageLiteral(resourceName: "backimg@3x.png")
let uiImageAlertAction = UIAlertAction(title: "", style: .default, handler: nil)
let scaleSze = CGSize(width: 245, height: 245/image.size.width*image.size.height)
let reSizedImage = image//.resize(newSize: scaleSze)
uiImageAlertAction.setValue(reSizedImage.withRenderingMode(.alwaysOriginal), forKey: "image")
alertView.addAction(uiImageAlertAction)
alertView.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.default, handler: nil))
alertView.addAction(UIAlertAction(title: "Edit", style: .default) {action in
let newImage = UIImagePickerController()
newImage.delegate = self
newImage.sourceType = UIImagePickerController.SourceType.photoLibrary
newImage.allowsEditing = false
})
self.present(alertView, animated: true, completion: nil)
}
/////////////////////below is details for tap gesture i have applied on //the label
taxImageView.translatesAutoresizingMaskIntoConstraints = false
taxImageView.textColor = tableTextColor
taxImageView.text = "View Image"
taxImageView.textAlignment = .left
taxImageView.attributedText = NSAttributedString(string: "View Image", attributes:
[.underlineStyle: NSUnderlineStyle.single.rawValue])
editInfoView.addSubview(taxImageView)
taxImageView.leftAnchor.constraint(equalTo: editInfoView.leftAnchor, constant: 260).isActive = true
taxImageView.topAnchor.constraint(equalTo: editInfoView.topAnchor, constant: 740).isActive = true
taxImageView.widthAnchor.constraint(equalToConstant: 300).isActive = true
taxImageView.heightAnchor.constraint(equalToConstant: 20).isActive = true
taxImageView.isUserInteractionEnabled = true
let taxImageGesture = UITapGestureRecognizer.init(target: self, action: #selector(taxImageTApped))
taxImageGesture.numberOfTapsRequired = 1
taxImageGesture.isEnabled = true
taxImageGesture.cancelsTouchesInView = false
taxImageView.gestureRecognizerShouldBegin(taxImageGesture)
taxImageView.addGestureRecognizer(taxImageGesture)
試試這個
在控制器中創建變量
let imagePickerController = UIImagePickerController()
將其添加到viewDidLoad中
override func viewDidLoad() {
super.viewDidLoad()
imagePickerController.delegate = self
imagePickerController.allowsEditing = true
imagePickerController.modalPresentationStyle = .popover
}
從所需位置調用此函數,也可以添加為手勢方法
func addActionSheet() {
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let galleryOption = UIAlertAction(title: "Choose Photo", style: .default, handler: { action in
self.imagePickerController.sourceType = .photoLibrary
self.present(self.imagePickerController, animated: true, completion: nil)
})
let cameraOption = UIAlertAction(title: "Take Photo", style: .default, handler: { action in
self.imagePickerController.sourceType = .camera
self.present(self.imagePickerController, animated: true, completion: nil)
})
let deleteOption = UIAlertAction(title: "Delete Photo", style: .default, handler: { action in
self.imageView.image = nil
})
let cancelOption = UIAlertAction(title: "Cancel", style: .cancel, handler: { action in
self.dismiss(animated: true, completion: nil)
})
alertController.addAction(galleryOption)
alertController.addAction(cameraOption)
alertController.addAction(deleteOption)
alertController.addAction(cancelOption)
self.present(alertController, animated: true, completion: nil)
}
添加UIImagePickerControllerDelegate
或UINavigationControllerDelegate
Delegate方法以獲取圖像
extension ControllerName: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
self.imageView.image = image
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
}
為Camera and Gallery創建NSObject類。
import UIKit
class CameraHandler: NSObject{
//MARK:- Shared Instance
static let shared = CameraHandler()
private override init() {
}
//MARK:- Variables
var currentVC: UIViewController!
//MARK: Internal Properties
var imagePickedBlock: ((UIImage) -> Void)?
//MARK:- Open Camera
func camera() {
if UIImagePickerController.isSourceTypeAvailable(.camera){
UINavigationBar.appearance().barTintColor = UIColor.black
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = .camera
currentVC.present(myPickerController, animated: true, completion: nil)
}
}
//MARK:- Open Gallery
func photoLibrary() {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
UINavigationBar.appearance().barTintColor = UIColor.black
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = .photoLibrary
currentVC.present(myPickerController, animated: true, completion: nil)
}
}
//MARK:- ActionSheet For Options
func showActionSheet(vc: UIViewController) {
currentVC = vc
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (alert:UIAlertAction!) -> Void in
self.camera()
}))
actionSheet.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { (alert:UIAlertAction!) -> Void in
self.photoLibrary()
}))
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
vc.present(actionSheet, animated: true, completion: nil)
}
}
//MARK:- UIImagePickerController Delegate Methods
extension CameraHandler: UIImagePickerControllerDelegate, UINavigationControllerDelegate{
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
UINavigationBar.appearance().barTintColor = UIColor.white
currentVC.dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
UINavigationBar.appearance().barTintColor = UIColor.white
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
self.imagePickedBlock?(image)
}else{
print("Something went wrong")
}
currentVC.dismiss(animated: true, completion: nil)
}
}
在視圖控制器中使用以下代碼:
在當前視圖控制器中顯示警報:
CameraHandler.shared.showActionSheet(vc: self)
要獲取所選圖像:
CameraHandler.shared.currentVC = self
CameraHandler.shared.imagePickedBlock = {(image) in
print(image)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.