Post

Replies

Boosts

Views

Activity

Thread 1: "Application tried to present modally a view controller that is already being presented by <UINavigationController
I am getting an error that crashes my app and takes me to AppDelegate file showing an error message Thread 1: "Application tried to present modally a view controller <MyApp.CanvasViewController: 0x7fca1c70f610> that is already being presented by <UINavigationController: 0x7fca1d837800> Basically what I was trying to do was to save an instance of UIViewController into app delegate protocol and then show every saved instance into a UITableView cell in which each cell will have a button to go back to that instance of the UIViewController. @objc func actbuttonFuncCall(_ sender: UIButton) { let index = sender.tag // access the content of the singleton class where the struct.view: UIVIewController is saved let identifier = VCs.shared.storedVc[index]?.view let vc = identifier print("tool bar button clicked! \(vc)") vc?.modalPresentationStyle = .fullScreen vc?.modalTransitionStyle = .coverVertical // set main CanvasViewController as default value present(vc ?? CanvasViewController(), animated: true) } That is to allow users to go back and forward into the main UIViewController where all the project functionality is executed. Is there anything I should consider doing to achieve my goal?
1
0
499
Jan ’24
Return an updated UIImageView
SayI have a function that will receive a UIImage as an argument, perform the logic inside it and then return it updated So I came up with something like this: func processImage(image: UIImage?) -> UIImage? { if let image = image, let cgImage = image.cgImage { let width = cgImage.width let height = cgImage.height let colorSpace = CGColorSpaceCreateDeviceRGB() let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue) if let context = CGContext(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 4 * width, space: colorSpace, bitmapInfo: bitmapInfo.rawValue) { context.draw(cgImage, in: CGRect(x: 0, y: 0, width: CGFloat(width), height: CGFloat(height))) if let data = context.data { let buffer = data.bindMemory(to: UInt8.self, capacity: width * height * 4) for i in 0..<(width * height) { let pixelIndex = i * 4 let red = buffer[pixelIndex] let green = buffer[pixelIndex + 1] let blue = buffer[pixelIndex + 2] if isSystemGrayPixel(red: red, green: green, blue: blue) { // Convert systemGray to systemPink buffer[pixelIndex] = 255 // R component buffer[pixelIndex + 1] = 182 // G component buffer[pixelIndex + 2] = 193 // B component } } if let modifiedCGImage = context.makeImage() { let processedImage = UIImage(cgImage: modifiedCGImage) return processedImage } } } } return image } insde it I have called a helper function that will conditionally check if it contains .systemGrey and if so than change it as I func isSystemGrayPixel(red: UInt8, green: UInt8, blue: UInt8) -> Bool { let systemGrayColor = UIColor.systemBlue let ciSystemGrayColor = CIColor(color: systemGrayColor) let tolerance: CGFloat = 10.0 / 255.0 let redDiff = abs(ciSystemGrayColor.red - CGFloat(red) / 255.0) let greenDiff = abs(ciSystemGrayColor.green - CGFloat(green) / 255.0) let blueDiff = abs(ciSystemGrayColor.blue - CGFloat(blue) / 255.0) return redDiff <= tolerance && greenDiff <= tolerance && blueDiff <= tolerance } When I try to save it into a saveCanvas I will show in the console that the entry is saved but when i try to retrieve it later I will get nil This is my saveCanvas to serve as a reference @objc func saveCanvas(_ sender: Any) { guard let canvas = Canvas(name: "", canvas: mainImageView, numOfPages: 0) else { return } var savedImageView2 = UIImageView() savedImageView2.image = mainImageView.image?.copy() as? UIImage let updatedImage = processImage(image: savedImageView2.image) canvas.canvas.image = updatedImage // array that stores UIimageView gobally defined canvasArray.append(canvas) if canvasArray.count > 0 { canvasArray.forEach{ canvas in print("\(canvas.canvas == savedImageView )") print("clicked button \( canvasArray) ") } } } I am expecting to retrieve each iteration of canvasArray when i call it later in another function(which worked fine before I created the processImage function) . Like I said the purpose of processImage is to check if my UIImage contains .systemGrey and i so returned updated as I defined inside my isSystemGrayPixel function. Is there anything you might consider I do different rather do in my processImage to make it work as expected?
2
0
659
Nov ’23
Detect a specific UIColor in a UIImage
I working on a drawing app using UIKit and I am trying to create a function that receives a UIImageView as an argument detects the color and then return that UIImageView updated. For example the UIImageView.image will contains a set of UIColors s and my detectColor will check if the image contains .systemGrey then will take any pixel that contains that specific color and change it to .systemPink and return the UIImageView updated For a beter explation here is an example of my function with pseudo code: func updateColor(_ imageView: UIImageView ) -> UIImageView { //.. initialize imageView //... if imageView.image color has pixels in .systemGrey color then //... change that color to .systemPink //... return updated imageView } Like I said I only want to change that specific color taking into account that in some cases my UIImageView.image multiple UIColors .Any Ideas, examples or suggestions will be much appreciated. Thanks!.
1
0
532
Nov ’23
Convert a UIImage into cgContext
I have a UIImageView that will serve as a drawing canvas and i want to implement a function that will get the content inside of UIImageView and convert it into a cgContect so it could be set as a background for the canvas to draw over but i ran into an issue. I have a reset button that will set the UIImageView.image to nil (to clear the current UIview) for that I tried to save the UIImageView into a canvasArray array to save it into a second view savedImageView this is my reset function that will delete the content of mainImageView @objc func resetPressed(_ sender: Any) { mainImageView.image = nil } The array that will is store and object that receives mainImageView as a param and the function that saved it var canvasArray: [Canvas] = [] @objc func saveCanvas(_ sender: Any) {. //here is the UIImageView guard let canvas = Canvas(name: "", canvas: mainImageView, numOfPages: 0) else { return } canvasArray.append(canvas) if canvasArray.count > 0 { canvasArray.forEach{ canvas in print("\(canvas.canvas.image)") } } } And the function that will get the iUIImageView.image and will convert it into cgContext @objc func changeImagefunc(_ sender: Any) { if canvasArray.count < pages { return } if let savedImage = canvasArray[pages].canvas.image { UIGraphicsBeginImageContext(view.frame.size) savedImage.draw(in: view.bounds) if let drawnImage = UIGraphicsGetImageFromCurrentImageContext() { mainImageView.image = drawnImage } UIGraphicsEndImageContext() print("clicked button ") } } I have 3 different UIViewImage that will ser a specific purpose mainImageView and tempImageView that will check the start and and of the drawing (function(which works fine) and the savedImageView that will receive the image converted into "cgContext " . Three of them stored in a function that then gets passed into the viewDidLoad section func setupImageViews() { view.addSubview(tempImageView) tempImageView.frame = view.bounds if savedImageView.image != nil { view.addSubview(savedImageView) savedImageView.frame = view.bounds } else { view.addSubview(mainImageView) mainImageView.frame = view.bounds } } Like I said i can not get this to work because the resetButton/function is deleted not only the current UIImageView (which is what is intended to do ) but also deletes the content inside the canvasArray which I want to avoid. Would there ve a different approach to it or what else do you recommend?
3
0
719
Nov ’23