6

I have a share extension that lets you crop an image and then upload it to our service. We call UIImageJPEGRepresentation to get the image's data before we upload it, but causes a crash due to memory excessive memory. This only happens with large images, and (as far as we can tell) on the SE, and didReceiveMemoryWarning is not called first. This is happening when using the Photos app.

Is there anyway to safely call UIImageJPEGRepresentation, or try to determine if the image is too large beforehand?

rob
  • 4,069
  • 3
  • 34
  • 41
  • Have you considered simply getting the image URL instead of loading the whole image into memory? – eshirima Dec 14 '16 at 20:44
  • The user can crop the photo before uploading it, so what we're converting to data is a `UIImage` created from a `CGContext`. – rob Dec 14 '16 at 21:17
  • 1
    @rob Did you ever find a solution to this? Struggling with this as well. – rocky Sep 11 '17 at 23:57
  • would also love a solution - happens all the time with HEIC images on iPhone X / Xs – Halpo Feb 14 '19 at 10:27

1 Answers1

0

Why not check for the image file size? If the image file size exceeds a certain quota, then resize it.

let image: Data = UIImagePNGRepresentation(image)
var imageSize: Double = (image.length)/1024 // in KB
eshirima
  • 3,837
  • 5
  • 37
  • 61