4

I'm developing an iOS today extension, that can read an image from UIPasteboard and save it on disk. This process fails with large images because iOS extensions can't use much memory. To workaround this issue, I'm checking the size of the image first and try to decide, if the widget can save it or should delegate this task to its host app:

let MAXIMUM_IMAGE_SIZE_BYTES = <SomeMagicNumber>

if let clipboardImage = UIPasteboard.generalPasteboard().image {
    let imageSize = CGImageGetHeight(clipboardImage.CGImage) * CGImageGetBytesPerRow(clipboardImage.CGImage)
    if imageSize > MAXIMUM_IMAGE_SIZE_BYTES {
        // Open host app to save image
    }
    else {
        // Save image directly
    }
}

I have the following questions:

  • Is my size calculation correct? I took it from this thread. I cannot instantiate a JPEG or PNG representation and read its size because of the memory limitations mentioned above.
  • Can I get rid of that magic number for the maximum image size in bytes? If not, are there any official specifications from Apple that I can use? I cannot test my app on every available iOS model and don't want to risk crashes on older devices.

Thanks a lot for your help!

Community
  • 1
  • 1
Felix
  • 776
  • 8
  • 16

1 Answers1

1

I'm just starting to look at the memory that a notification service extension is using. I found this presentation. Might be helpful for others.

https://cocoaheads.tv/memory-use-in-extensions-by-conrad-kramer/

What was your solution to this issue?

TALE
  • 960
  • 13
  • 22
  • I'm still using my old logic. Thanks for the link, I will take a look. If you come up with a better solution, please let me know. – Felix Mar 24 '17 at 15:24