Reputation: 1321
A Demo Project -
I have an UIImageView and three buttons.
Whenever I have clicked any of button, I am changing image of the image view. My images are of 1242*1242 in size.
Problem: Every first when I have clicked the button it took memory 5 MB. For example, I have clicked button 1, memory increase by 5 MB and then I have pressed button 2, memory increases by 5 MB. But when I have clicked button 1 again no memory increased.
Is this usual behavior of UIImage View? Actually, I am working on Photo Frames and each time when I have applied new frame memory is increased. Due to this after applying 30-40 frames app crashes.
Can someone refer how to work on such type of photo frame apps or any reason why is UIImagesView taking memory?
Thanks in advance!
Upvotes: 0
Views: 227
Reputation: 428
Yes @Amanpreet, I am also getting the same behavior. While investigating in the instrument I found that some chunk of memory as ImageIO-jpeg-data allocating every time I load a new image on button click.
---------------------SOLUTION-----------------------------
Load image in imageView with
UIImage(contentsOfFile: filePath!)
method. And now your memory size will not increase every time with new image.
EXAMPLE CODE
@IBAction func loadOne(_ sender: UIButton) {
let filePath = Bundle.main.path(forResource: "1", ofType: "jpg")
imageView.image = UIImage(contentsOfFile: filePath!)
}
@IBAction func loadTwo(_ sender: UIButton) {
let filePath = Bundle.main.path(forResource: "2", ofType: "jpg")
imageView.image = UIImage(contentsOfFile: filePath!)
}
@IBAction func loadThree(_ sender: UIButton) {
let filePath = Bundle.main.path(forResource: "3", ofType: "jpg")
imageView.image = UIImage(contentsOfFile: filePath!)
}
i am using images of size 1920*1280 , Previously my memory usage increases like 41.6 MB, 51.5 MB, 59.5 MB, 67.5 MB, after every click, But after adopting solution given above memory usage stucks at 50.4 MB
Upvotes: 1