我有一个xcode的问题:我创建了一个绘图应用程序,你可以在图片上绘图,当我打开图片然后我点击绘图,图像拉伸, 如果我更改内容模式(aspectFit,aspectFill) 我忘了什么? 如何正确设置内容模式?
这是我的代码:
import UIKit
import Photos
class ViewController: UIViewController {
@IBOutlet var imageView: UIImageView!
@IBOutlet var toolIcon: UIButton!
@IBOutlet var tempImage: UIImageView!
var lastPoint = CGPoint.zero
var swiped = false
var red: CGFloat = 0.0
var green: CGFloat = 0.0
var blue: CGFloat = 0.0
var brushWidth:CGFloat = 10.0
var opacity:CGFloat = 1.0
var tool: UIImageView!
var isDrawing = true
var selectedImage: UIImage!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
imageView.contentMode = UIViewContentMode.scaleAspectFill
tempImage.contentMode = UIViewContentMode.scaleAspectFill
tool = UIImageView()
tool.frame = CGRect(x:self.view.bounds.size.width, y: self.view.bounds.size.height, width: 38, height: 38)
//tool.image = #imageLiteral(resourceName: "Pen")
self.view.addSubview(tool)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){
swiped = false
if let touch = touches.first {
lastPoint = touch.location(in: self.view)
}
}
func drawLines(fromPoint:CGPoint, toPoint:CGPoint){
UIGraphicsBeginImageContext(self.view.frame.size)
tempImage.image?.draw(in: CGRect(x:0, y:0, width: self.view.frame.width,height: self.view.frame.height))
let context = UIGraphicsGetCurrentContext()
context?.move(to:CGPoint(x:fromPoint.x,y:fromPoint.y))
context?.addLine(to: CGPoint(x:toPoint.x, y: toPoint.y))
tool.center = toPoint
context?.setBlendMode(CGBlendMode.normal)
context?.setLineCap(CGLineCap.round)
context?.setLineWidth(brushWidth)
context?.setStrokeColor(UIColor(red: red, green: green, blue: blue, alpha: 1.0).cgColor)
context?.strokePath()
tempImage.image = UIGraphicsGetImageFromCurrentImageContext()
tempImage.alpha = opacity
UIGraphicsEndImageContext()
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?){
swiped = true
if let touch = touches.first {
let currentPoint = touch.location(in: self.view)
drawLines(fromPoint: lastPoint, toPoint: currentPoint)
lastPoint = currentPoint
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if !swiped {
drawLines(fromPoint: lastPoint, toPoint: lastPoint)
}
// Merge tempImageView into mainImageView
UIGraphicsBeginImageContext(imageView.frame.size)
imageView.image?.draw(in: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendMode: CGBlendMode.normal, alpha: 1.0)
tempImage.image?.draw(in: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendMode: CGBlendMode.normal, alpha: opacity)
imageView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
tempImage.image = nil
}
@IBAction func reset(_ sender: Any) {
self.imageView.image = nil
}
@IBAction func colorPicked(_ sender: UIButton) {
if sender.tag == 0 {
(red,green,blue) = (1,0,0)
}else if sender.tag == 1 {
(red,green,blue) = (0,1,0)
}else if sender.tag == 2{
(red,green,blue) = (0,0,1)
}else if sender.tag == 3 {
(red,green,blue) = (1,0,1)
}else if sender.tag == 4{
(red,green,blue) = (1,1,0)
}else if sender.tag == 5 {
(red,green,blue) = (0,1,1)
}else if sender.tag == 6{
(red,green,blue) = (1,1,1)
}else if sender.tag == 7 {
(red,green,blue) = (0,0,0)
}
}
@IBAction func save(_ sender: Any) {
let actionSheet = UIAlertController(title: "Pick your options", message: "", preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Pick an image", style: .default, handler: { (_) in
let imagePicker = UIImagePickerController()
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = false
imagePicker.delegate = self
self.present(imagePicker, animated: true, completion: nil)
}))
actionSheet.addAction(UIAlertAction(title: "Save your drawing", style: .default, handler: {(_) in
if let image = self.imageView.image {
UIImageWriteToSavedPhotosAlbum(image, nil,nil,nil)
}
}))
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .default, handler: nil))
present(actionSheet, animated: true, completion: nil)
}
@IBAction func erase(_ sender: Any) {
if (isDrawing) {
(red,green,blue) = (1,1,1)
tool.image = #imageLiteral(resourceName: "Gomma")
toolIcon.setImage(#imageLiteral(resourceName: "Pen"), for: .normal)
}else {
(red,green,blue) = (0,0,0)
tool.image = nil
toolIcon.setImage(#imageLiteral(resourceName: "Gomma"), for: .normal)
}
isDrawing = !isDrawing
}
@IBAction func setting(_ sender: Any) {
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
let settingsVc = segue.destination as! SettingViewController
settingsVc.delegate = self
settingsVc.red = red
settingsVc.green = green
settingsVc.blue = blue
settingsVc.brush = brushWidth
settingsVc.opacity = opacity
}
}
extension ViewController:UINavigationControllerDelegate,UIImagePickerControllerDelegate,SettingsVCDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let imagePicked = info[UIImagePickerControllerOriginalImage] as? UIImage {
self.selectedImage = imagePicked
self.imageView.image = selectedImage
self.tempImage.image = selectedImage
dismiss(animated: true, completion: nil)
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
func settingsViewControllerDidFinish(_settingsVc: SettingViewController) {
self.red = _settingsVc.red
self.green = _settingsVc.green
self.blue = _settingsVc.blue
self.brushWidth = _settingsVc.brush
self.opacity = _settingsVc.opacity
}
}