ҚОРҒАУҒа жіберілді


Ә қосымшасы (міндетті) Программа листингі 1 QR кодын құру



бет27/28
Дата09.05.2022
өлшемі3,52 Mb.
#33474
түріДиплом
1   ...   20   21   22   23   24   25   26   27   28
Байланысты:
Диплом Жандарбек

Ә қосымшасы

(міндетті)


Программа листингі

1 QR кодын құру

class GeneratorViewController: UIViewController, UITextViewDelegate {

@IBOutlet weak var scrollView: UIScrollView!

@IBOutlet weak var imageView: UIImageView!

@IBOutlet weak var textView: UITextView!

@IBOutlet weak var correctionLevelSegmentControl: UISegmentedControl!

override func viewDidLoad() {

super.viewDidLoad()

self.textView.delegate = self

self.registerForKeyboardNotifications()

let longPress = UILongPressGestureRecognizer(target: self, action: #selector(self.shareImage))

self.imageView.addGestureRecognizer(longPress)

self.imageView.isUserInteractionEnabled = true

self.refreshQRCode()

self.hideKeyboardWhenTappedAround()

}

@IBAction func correctionLevelChanged(_ sender: Any) {



self.refreshQRCode()

}

func textViewDidChange(_ textView: UITextView) {



self.refreshQRCode()

}


func refreshQRCode() {

let text:String = self.textView.text

// Generate the image

guard let qrCode:CIImage = self.createQRCodeForString(text) else {

print("Failed to generate QRCode")

self.imageView.image = nil

return

}

let viewWidth = self.imageView.bounds.size.width;



let scale = viewWidth/qrCode.extent.size.width;

let scaledImage = qrCode.transformed(by: CGAffineTransform(scaleX: scale, y: scale))

self.imageView.image = UIImage(ciImage: scaledImage)

}

func createQRCodeForString(_ text: String) -> CIImage?{



let data = text.data(using: .utf8)

let qrFilter = CIFilter(name: "CIQRCodeGenerator")

qrFilter?.setValue(data, forKey: "inputMessage")

Ә қосымшасының жалғасы
let values = ["L", "M", "Q", "H"]

let index = max(0, min(self.correctionLevelSegmentControl.selectedSegmentIndex, (values.count-1)))

let correctionLevel = values[index]

qrFilter?.setValue(correctionLevel, forKey: "inputCorrectionLevel")

return qrFilter?.outputImage

}

@objc func shareImage() {



guard let image = self.imageView.image else {

return


}

let activityViewController = UIActivityViewController(activityItems: [ self.sharableImage(image) ], applicationActivities: nil)

activityViewController.popoverPresentationController?.sourceView = self.imageView

self.present(activityViewController, animated: true, completion: nil)

}

func sharableImage(_ image: UIImage) -> UIImage{



let renderer = UIGraphicsImageRenderer(size: image.size, format: image.imageRendererFormat)

let img = renderer.image { ctx in

image.draw(at: CGPoint.zero)

}

return img



}

func registerForKeyboardNotifications(){

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWasShown), name: UIResponder.keyboardDidShowNotification, object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillBeHidden), name: UIResponder.keyboardWillHideNotification, object: nil)

}

@objc func keyboardWasShown(_ aNotification: NSNotification) {



let info = aNotification.userInfo!

let kbSize:CGSize = (info[UIResponder.keyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue.size

let contentInsets:UIEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: kbSize.height, right: 0)

self.scrollView.contentInset = contentInsets

self.scrollView.scrollIndicatorInsets = contentInsets

var aRect:CGRect = self.view.frame

aRect.size.height -= kbSize.height

Ә қосымшасының жалғасы

if (!aRect.contains(self.textView.frame.origin)) {

let scrollPoint:CGPoint = CGPoint(x: 0, y: self.textView.frame.origin.y-kbSize.height)

self.scrollView.setContentOffset(scrollPoint, animated: true)

}

}

@objc func keyboardWillBeHidden(_ aNotification: NSNotification) {



let contentInsets = UIEdgeInsets.zero

self.scrollView.contentInset = contentInsets;

self.scrollView.scrollIndicatorInsets = contentInsets;

}

func hideKeyboardWhenTappedAround() {



let tapGesture = UITapGestureRecognizer(target: self,

action: #selector(hideKeyboard))

tapGesture.cancelsTouchesInView = false

self.view.addGestureRecognizer(tapGesture)

}

@objc func hideKeyboard() {



self.view.endEditing(true)

}

}





Достарыңызбен бөлісу:
1   ...   20   21   22   23   24   25   26   27   28




©emirsaba.org 2024
әкімшілігінің қараңыз

    Басты бет