fayyaz
fayyaz

Reputation: 75

UIImageJPEGRepresentation using large amount of memory (Swift 3.0)

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)

Upvotes: 3

Views: 1220

Answers (3)

Marco Santarossa
Marco Santarossa

Reputation: 4066

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.

Upvotes: 1

Brijesh Shiroya
Brijesh Shiroya

Reputation: 3383

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

}

Upvotes: 0

Johnson Nira
Johnson Nira

Reputation: 1

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.

Upvotes: 0

Related Questions