3

I'm trying to compress and get the NSdata from between 20 and 30 UIImages with a "for-loop" like this:

for theImage in selectedUIImages {

let data = UIImageJPEGRepresentation(image,0.5)
// doing something with the data

}

Tried on an iPhone 7 with no issues besides my app using upto 700MB of memory when going through the loop, but on an older iPhone I get the message:

*Message from debugger: Terminated due to memory issue.*

The main objective is to get the NSData from the UIImage so I can put the image in a dir for uploading. Let me explain:

The Amazon S3 Transfer utility wants a path/url to the image and therefore I need to make a path/url for the UIImage and the only way i know is to get it by:

data.write(to: URL(fileURLWithPath: localPath), options: .atomic)
NSNoob
  • 5,548
  • 6
  • 41
  • 54
fayyaz
  • 75
  • 2
  • 11
  • Did you try it in device ? – Priyal Feb 21 '17 at 09:14
  • Possible duplicate of http://stackoverflow.com/questions/25248294/uiimagejpegrepresentation-received-memory-warning. – Martin R Feb 21 '17 at 09:17
  • Priyal: What do u mean? – fayyaz Feb 21 '17 at 09:39
  • Martin: This is for swift 3.0 – fayyaz Feb 21 '17 at 09:39
  • @NSNoob Thx for asking mate :-) let me explain. The Amazon S3 Transfer utility wants a path/url to the image and therefor i need to make a path/url for the UIimage and the only way i know is to get it by: data.write(to: URL(fileURLWithPath: localPath), options: .atomic) – fayyaz Feb 21 '17 at 09:50
  • @fayyaz See now this is a classic [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) Your main problem is figuring out the path to a image, not moving them in a loop to documents directory. – NSNoob Feb 21 '17 at 09:56
  • i see what you mean :-) – fayyaz Feb 21 '17 at 09:57
  • @fayyaz I would suggest using Photos Framework and see [This](http://stackoverflow.com/questions/26025487/nsurl-from-phasset) question on getting file path from the PHAsset. It will save you memory and processing power – NSNoob Feb 21 '17 at 09:58
  • @fayyaz how you solved this facing same issue – Pooja M. Bohora Oct 23 '18 at 11:14
  • @PoojaM.Bohora When using PHImageManager.default().requestImage i used targetsize to get the images with smaller widht and height instead of the big originals. it saved me a lot of memory – fayyaz Nov 03 '18 at 14:07

3 Answers3

1

Try using an autorelease pool:

autoreleasepool {
    for theImage in selectedUIImages {

        let data = UIImageJPEGRepresentation(image,0.5)
        // doing something with the data

     }
}

and move it in a background thread.

Marco Santarossa
  • 4,058
  • 1
  • 29
  • 49
0

Because your app run out of memory. You can save it to Document directory after compress then upload it to server one by one. So it not make your memory issue.

  • So it would be better to first compress then upload. So basically 1. a for-loop that compresses and then another that uploades? – fayyaz Feb 21 '17 at 09:42
0

You can decrease the image size by decreasing a ratio parameter. You can use 0.3 instead 0.5.

for theImage in selectedUIImages {

let data = UIImageJPEGRepresentation(image,0.3)
// doing something with the data

}
Brijesh Shiroya
  • 3,323
  • 1
  • 13
  • 20
  • same error as before :( *Message from debugger: Terminated due to memory issue.* – fayyaz Feb 21 '17 at 09:38
  • @fayyaz You will keep getting that message because you have no control over the number of images in selectedUIImages array. It may work for a number of images and start crashing on just one more image than that. – NSNoob Feb 21 '17 at 09:47