Thursday, April 30, 2026
HomeiOS Developmentios - Swift - Picture View must be fetched and displayed with...

ios – Swift – Picture View must be fetched and displayed with identical scaled worth which can be retrieved from database

[ad_1]

I’ve a view inside the primary view of a viewcontroller. Inside that i’m creating one other view with customized width and top(canvas to attract)

I’m creating and fetching picture view.

Create Sticker –

func createSticker() {
        let stickerImageString = imagesForSticker
        let stickerImage_View: UIImageView = {
            let stickerView = UIImageView()
            stickerView.body = CGRect(x: resultedView.middle.x, y: resultedView.middle.x, width: 120.0, top: 120.0)
            do {
                strive stickerView.picture = UIImage(information: NSData(contentsOf: NSURL(string: stickerImageString)! as URL) as Information)
            }catch {
                print("Cant get sticker url to transform into picture")
            }
            stickerView.contentMode = .scaleAspectFill
            stickerView.translatesAutoresizingMaskIntoConstraints = false
            stickerView.isUserInteractionEnabled = true
            stickerView.clipsToBounds = true
            return stickerView
        }()
        
        if UserDefaults.customary.bool(forKey: "stickerSelected") == true {
            let tapGestureRecognizer = UITapGestureRecognizer(goal: self, motion: #selector(stickerTapped(tapGestureRecognizer:)))
            stickerImage_View.isUserInteractionEnabled = true
            stickerImage_View.addGestureRecognizer(tapGestureRecognizer)            
        }
        self.stickerImageTestView = stickerImage_View
        
        databaseHandlerObj.saveStickerImage(stickerProjectId: self.projectTokenID, selectedSticker: stickerImageTestView.picture!,stickerImageURL: stickerImageString ,imageX: stickerImageTestView.middle.x, imageY: stickerImageTestView.middle.y, width: stickerImageTestView.body.width, top: stickerImageTestView.body.top, imageScale: 1.0, rotationAngle: rotationAngle)
        
        self.createdStickerArray.append(stickerImage_View)
        UserDefaults.customary.set(false, forKey: "stickerSelected")
    }

Fetch Sticker from database-

    func fetchStickers() {
        databaseHandlerObj.fetchStickerImageData()
        let arrayOfSticker = databaseHandlerObj.stickerDataFetch
        for merchandise in 0..<arrayOfSticker.depend {
            self.fetchStickerProjectId = arrayOfSticker[item].stickerProjectId
            
            if UserDefaults.customary.bool(forKey: "fromSingleProject") == true {
                if fetchStickerProjectId == getSingleProjectID {
                    self.fetchStickerId = arrayOfSticker[item].stickerId
                    self.picture = UIImage(information: arrayOfSticker[item].stickerImage! as Information)!
                    self.fetchedImageURL = arrayOfSticker[item].stickerImageURL!
                    self.imageX = CGFloat(arrayOfSticker[item].imageX)
                    self.imageY = CGFloat(arrayOfSticker[item].imageY)
                    self.top = CGFloat(arrayOfSticker[item].top)
                    self.width = CGFloat(arrayOfSticker[item].width)
                    self.imageScale = CGFloat(arrayOfSticker[item].imageScale)
                    self.rotationAngle = CGFloat(arrayOfSticker[item].rotationAngle)
                    
                   // let imageView: UIImageView = {
                        let imageV = UIImageView()
                        imageV.body = CGRect(x: imageX, y: imageY, width: width, top: top)
                        imageV.picture = self.picture
                        imageV.remodel = CGAffineTransform(scaleX: imageScale, y: imageScale)
                        imageV.remodel = CGAffineTransform(rotationAngle: rotationAngle)
                        imageV.contentMode = .scaleAspectFill
                        imageV.clipsToBounds = true
                        imageV.tag = Int(fetchStickerId)
                        UserDefaults.customary.set(fetchStickerId, forKey: "stickerID")
                    //    return imageV
                   // }()
                    
                   // if UserDefaults.customary.bool(forKey: "canvasLock") == false {
                        let tapGestureRecognizer = UITapGestureRecognizer(goal: self, motion: #selector(stickerTapped(tapGestureRecognizer:)))
                    imageV.isUserInteractionEnabled = true
                    imageV.addGestureRecognizer(tapGestureRecognizer)
//                        let pinchGesture = UIPinchGestureRecognizer(goal: self, motion: #selector(pinchStickerAction))
//                        imageView.addGestureRecognizer(pinchGesture)
//                        let rotate = UIRotationGestureRecognizer(goal: self, motion: #selector(rotateStickerAction))
//                        imageView.addGestureRecognizer(rotate)
                    //}
                    
                    self.stickerImageTestView = imageV
                    let imageView = StickerView(contentView: imageV, configuration: configuration)
                    //createPanGestureRecognizer_Sticker(targetView: imageView)
                    createdView.addSubview(imageView)
                }
            }

I’m utilizing a SwiftStickerView library to scale, rotate, pan the imageView inside my view.
https://github.com/native-mobile-app-developers/SwiftStickerView

I’m able to carry out the remodel and save the values to the database. The view seems excellent throughout performing the scaling, panning and rotating capabilities. However as soon as I put it aside and choose the undertaking once more, when the fetchSticker will get fetched with earlier saved values disturbs the scaled, panned values for the picture view, both it’s larger or smaller than it was earlier when accomplished realtime.

I wish to understand how can I maintain the scaled or panned values identical even once I fetch it again after saving.

Only for fast take a look at the scaledGesture including code right here (you possibly can check out entire file at above talked about github hyperlink(library))-

@objc func scaleGesture(_ recognizer: UITapGestureRecognizer) {
        let touchLocation = recognizer.location(in: self.superview)
        let middle = self.middle
        
        swap recognizer.state {
        case .started:
            self.initialBounds = self.bounds
            self.initialDistance = CGPointGetDistance(point1: middle, point2: touchLocation)
            if let delegate = self.delegate {
                delegate.stickerViewDidBeginScale(self)
            }
        break
        case .potential:
        break
        case .modified:
            scale = CGPointGetDistance(point1: middle, point2: touchLocation) / self.initialDistance
            let minimumScale = self.configuration.minimumSize / min(self.initialBounds.dimension.width, self.initialBounds.dimension.top)
            scale = max(scale, minimumScale)
            let scaledBounds = CGRectScale(self.initialBounds, wScale: scale, hScale: scale)
            self.bounds = scaledBounds
            self.setNeedsDisplay()
            
            if let delegate = self.delegate {
                delegate.stickerViewDidChangeScale(self)
            }
        break
        case .ended:
        break
        case .cancelled:
        break
        case .failed:
        break
        }
    }

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments