timgcarlson timgcarlson - 6 months ago 108
iOS Question

Change the preview image in an SLComposeServiceViewController

I have created a Safari Share Extension where I get a specific image from the current URL. That part is done but now I am trying to replace the preview image on the SLComposeServiceViewController with the one I scraped from the URL. I cannot seem to find a way to change the preview image that is automatically generated by SLComposeServiceViewController

enter image description here

How can I change this image?

I've tried looking for at the

inputItems
property of
NSExtenstionContext
but only can see the URL in there.

extension NSExtensionContext {
for item in self.inputItems {
if let extenstionItem = item as? NSExtensionItem {
print("attachments = \(extenstionItem.attachments)")
}
}
}


attachments = Optional([<NSItemProvider: 0x12d513e70> {types = (
"public.url"
)}])


I'm hoping that I can change this image without having to create my own view since this is the only change I need to make to the default.

Even being able to hide the image placed there by default would be helpful!

Answer

I found the way to accomplish this. You override loadPreviewView() in the SLComposeServiceViewController subclass and return the UIImageView (or any view) with the image. To update it during the same runtime, you keep a reference to it in the class.

override func loadPreviewView() -> UIView! {        
  imagePreviewView = UIImageView(image: UIImage(named: "imageName"))
  return imagePreviewView
}
Comments