0

I work on a swift application that allows to take a picture and embeds some text on it (for example: the current date). I have some labels (containing the current date, for example) appearing on the screen when I take my picture.

Thanks to this topic, I succeeded at embedding my text on my UIImage. Now, when I load my image in another ViewController, I want to permit the user to move the text with his finger on the image and to change its color, so I need to get back the control on my block of text. How can I do it ? Can somebody please give me some help ?

I think there might be some clue in the Ray Wenderlich tutorial.

Community
  • 1
  • 1
Zigzag
  • 11

2 Answers2

0
  1. Create a custom view inherit from UIView
  2. Add a subview to it and keed a reference on this subview (UILabel, UITextView, etc.)
  3. Use touch-handling methods from UIView to move your subview to the tapped positon.

for example :

class MyCustomView : UIView {
    var backgroundImage: UIImageView!
    var dateLabel: UILabel!

    init() {
        // ... do job to handle image and create subviews
    }

    func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
         if let aTouch = touches.first {
              dateLabel.position = aTouch.locationInView(self)
         }
    }
}

This code need to be fullfilled but you have an algorithm and an example.

Vincent

Vincent Saluzzo
  • 1,377
  • 1
  • 11
  • 13
  • Of course, I missed out a point, you can render a view to export it as a UIImage and save it as JPG/PNG with usual methods – Vincent Saluzzo May 12 '15 at 14:25
  • Thanks for your help. I finally changed my mind and decided to embed the text only once the user moved it and clicked on the "done" button on my UI. Therefore, the user can only move my labels once. – Zigzag Jun 23 '15 at 09:12
0

Instead of drawing text on the image and saving it to disk as a JPG/PNG file, you can create a UIView with a UIImageView and a UILabel in it. Saving the UIView as an image in the Camera roll means taking a snapshot of the view and saving that to disk in your desired image format; but at the same time, also save to disk the exact same UIView using NSKeyedArchiver.

That way, you can inflate the UIView from NSKeyedUnarchiver whenever you need to make the "image" (which is a UIView) editable again.

See:

How Do I Take a Screen Shot of a UIView?

How to serialize a UIView?

Community
  • 1
  • 1
Matthew Quiros
  • 13,385
  • 12
  • 87
  • 132