elvetico Segnala messaggio Inviato Marzo 13, 2017 (modificato) Ciao a tutti Ho bisogno di un aiuto,ho realizzato un app tipo Draw (per intenderci da disegnare) Ho implementato anche la possibilità di importare una foto dal rullino e poter disegnare sulla foto stessa e naturalmente salvarla E qui il problema, L'imageView viene impostata nel content Mode su AspectFill in modo da avere un immagine grande come lo schermo, ma quando faccio clic per disegnare sulla foto questa si allunga, deformandola e se la salvo mi appare deformata,e la stessa cosa succede se imposto qualsiasi altro modo(aspectFit,Center, ecc ecc) Qualcuno che mi può aiutare? Modificato Marzo 13, 2017 da elvetico Condividi questo messaggio Link di questo messaggio Condividi su altri siti
Maghetto Segnala messaggio Inviato Marzo 13, 2017 Ma la dimensione dell'immagine viene modificata quando si disegna? Si disegna sulla stessa o su un'altra? Perché in tal caso dovresti prima settare il contentMode anche per quella view. Comunque sentiamo cosa dicono gli altri, non è il mio campo. Condividi questo messaggio Link di questo messaggio Condividi su altri siti
elvetico Segnala messaggio Inviato Marzo 14, 2017 (modificato) la dimensione rimane la stessa in pratica dovrebbe aprirsi cosi come la apri in foto sull'iPhone, il problema come dicevo é che quando tocco lo schermo per iniziare a disegnare l'immagine si allunga,indipendentemente da come imposto il contentMode Volevo aggiungere che l'errore é quasi sicuramente nel metodo touches, perché ho provato con un ImageView inserendo un immagine questa viene mostrata correttamente Ma non capisco dove sbaglio aggiungo il codice se qualcuno mi aiuta a capire l'errore: 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 = .scaleAspectFit //tempImage.contentMode = .scaleAspectFit imageView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleBottomMargin, .flexibleRightMargin, .flexibleLeftMargin, .flexibleTopMargin] imageView.contentMode = UIViewContentMode.scaleAspectFit imageView.clipsToBounds = true tempImage.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleBottomMargin, .flexibleRightMargin, .flexibleLeftMargin, .flexibleTopMargin] tempImage.contentMode = UIViewContentMode.scaleAspectFit tempImage.clipsToBounds = true */ 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 /* self.imageView.contentMode = UIViewContentMode.scaleAspectFit self.imageView.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 } } Modificato Marzo 14, 2017 da elvetico aggiunto informazione Condividi questo messaggio Link di questo messaggio Condividi su altri siti
Maghetto Segnala messaggio Inviato Marzo 14, 2017 contentMode appare sempre commentato, è giusto oppure hai commentato il codice per fare prove? Condividi questo messaggio Link di questo messaggio Condividi su altri siti
elvetico Segnala messaggio Inviato Marzo 14, 2017 Ho commentato io per delle prove, ma con nessuno dei contentMode funzionano Condividi questo messaggio Link di questo messaggio Condividi su altri siti