Media-attachment crops image in ios 10 notification
Asked Answered
H

2

7

iOS10 notifications allow us to add images as media-attachments to them. Unfortunately, I haven't found any good way to control attachment's appearance inside the notification.

For example, I'm adding as attachment this image: enter image description here

And it shows as:

enter image description here

I'm passing square-images and want to avoid image-crop (as you can see one ear of cat has been cut).

I'm sending notifcation (as a local one) via this snippet:

    let content = UNMutableNotificationContent()
    content.title = "Test notification"
    content.body = "Test notification"
    content.categoryIdentifier = "myNotificationCategory"

    let attachement = try! UNNotificationAttachment(identifier: "image",
                                                                url: Bundle.main.url(forResource: "cat", withExtension: "png")!,
                                                                options: nil)

    content.attachments = [ attachement ]
    let request = UNNotificationRequest(identifier:requestIdentifier, content: content, trigger: nil)
    UNUserNotificationCenter.current().delegate = self
    UNUserNotificationCenter.current().add(request){(error) in
        if (error != nil){
        }
    }

So the questions are:

  • Can I avoid image crop? (If not - how to remove image at all?)
  • Bonus question: is there a way to show 2 media-attachments in one notification (while it's collapsed)

Thanks!

Humidity answered 22/8, 2016 at 18:59 Comment(3)
Maybe with the UNNotificationAttachmentOptionsThumbnailClippingRectKey to put in the option parameter to the UNNotificationAttachment creation method? See the doc for more info.Wince
@Wince thanks for the comment! Doc looks promising, but in the real life never managed to make it work (Xcode-beta5, iOS 10 beta 5). Have you tried it?Humidity
I up-voted because of the cat. (and good question of course).Ringo
I
4

You should - as @larme comments - be able to use UNNotificationAttachmentOptionsThumbnailClippingRectKey. However, there seems to be a bug there somewhere:

Infamy answered 23/8, 2016 at 6:51 Comment(0)
S
0

I return to this very old thread… The solution I have found (not gorgeous !), is to create another image (in resource) with a square format. Because the rect is in fact a square that clips the original.

Smooth answered 29/9, 2020 at 23:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.