Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add stickers to an image in Swift?

Tags:

ios

swift

iphone

I'm working on a Swift iOS app where I want the user to be able to place an image (a sticker such as a beard or an eyepatch) over another image.

Currently I have it working to where they can take a photo or pull an image up from their Photos and have that display in a UIImage. Obviously next is adding the sticker. I've been able to dig up zero data on this from Google or Stack. And I mean zero.

Does anyone have a general idea of how I would execute this? Specifics would help too of course.

like image 627
user3246092 Avatar asked Sep 05 '15 02:09

user3246092


People also ask

How can I put stickers on my photos for free?

Fotor is a free online photo editor that lets you easily add stickers to photos. Just upload your picture and you can immediately start decorating it with stickers. There're tons of stickers available on Fotor. Choose stickers you like and drag them to your photos.

How do I add stickers to iOS photos?

To add a sticker to your photo, just slide to pick your pack, tap to open the game or app to see the stickers, and slide up to view them all if there are several. Tap to stick one on your photo, move it where you like, close the sticker window, and hit the capture button.


1 Answers

Here's the easy way.

  1. have a UIView as a parent, let's say stickerView
  2. add image to stickerView
  3. add stickers to stickerView
  4. save a snapshot of stickerView

Here's the code to snapshot UIView,

extension UIImage {
    class func imageWithView(view: UIView) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0)
        view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
        let img = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return img
    }
}

Usage

let snapshotImage = UIImage.imageWithView(stickerView)

like image 109
nRewik Avatar answered Sep 18 '22 17:09

nRewik