0

I am making an iMessage app with images for a keyboard app like custom emojis. I have setup outlets on my iMessage story board and connected the button. In my MessageViewController I have the code below in my IBOulet. I want to resize the image smaller but I can't seem to figure this one out. Any help is greatly appreciated!

@IBAction func button(_ sender: Any) { 
    label.text = "button pressed"
    let layout = MSMessageTemplateLayout()
    layout.image = UIImage(named: "270a.png")
    let message = MSMessage()
    message.layout = layout
    activeConversation?.insert(message, completionHandler: nil) 
}
Rohan Sanap
  • 2,773
  • 2
  • 21
  • 39

1 Answers1

0

When you add an image to message template you can request a width and height, but it will scale the image up or down (respecting the aspect ratio) to a size that it thinks is best.

If the image resource you have isn't the size you want, you can attempt to create a new image in memory, but MSMessageTemplateLayout will modify this as it sees fit.

let original = UIImage(named: "background")

// use CGContext to create new image in memory
// 10 x 10 is super small, so messages app will scale this up
let image = CGSize(width: 10, height: 10).image { context, frame in

    original?.draw(in: frame, blendMode: .luminosity, alpha: 1)
}

let message = MSMessage()
let layout = MSMessageTemplateLayout()

layout.image = image
message.layout = layout

self.activeConversation?.insert(message, completionHandler: nil)

I like to use this extension to make it a little easier working with CGContext: https://gist.github.com/mathewsanders/94ed8212587d72684291483905132790

MathewS
  • 2,267
  • 2
  • 20
  • 31
  • Thank you, going to give this a shot... I am trying to make an app like kimoni using custom "emojis". maybe I am tackling this the wrong way but I think I am on the correct path. lol We will see. – Matthew Montanez Jan 07 '17 at 03:29
  • You might want to consider using `MSSticker` rather than `MSMessage` because it allows more flexibility in placing you assets on different parts of the screen. You can create this in code if you need to grab the image assets at runtime, but if you're all part of your bundle you can make a sticker pack without evening writing any code! Here's a good tutorial if you take the runtime approach: http://willowtreeapps.com/blog/imessage-apps-part-one/ – MathewS Jan 07 '17 at 03:46
  • Thank you, I have it setup in stickers but just wasn't exactly what i was looking for i want more control of the look. I ended up finding this and it looks like I should be using the pasteboard, actually makes my code a lot shorter just need to focus on the design and scrolling through pages. just need to make sure I can get the pictures small like the emojis. if not quess I'll go the route you suggested with stickers :-) http://stackoverflow.com/questions/31741015/add-stickers-in-custom-keyboard-extension/37019342#37019342 – Matthew Montanez Jan 07 '17 at 04:01