Motivation gym5
Motivation gym5

Reputation: 291

Terminated app due to Memory error

Everything was fine but when I connected my app with my device.

After I see some images by clicking on a button this error comes.

I am using many images so when person clicks on a button it shows random different image.

I don't know why its showing this error, I looked at many questions similar to this but did not find any solution.

override func viewDidLoad() {
    super.viewDidLoad()
    ButtonStyle()


    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
    self.navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.translucent = true



    let quote = randomQuote()
    QuotesLabel.text = quote
    QuotesLabel.textColor = UIColor.whiteColor()
    QuotesLabel.font = UIFont.systemFontOfSize(30)
    QuotesLabel.text = QuotesLabel.text?.uppercaseString
    QuotesLabel.numberOfLines = 0

    // random background image
    var random = arc4random_uniform(20)
    switch(random){

    case 0: ChangingBackground.image = UIImage(named: "backgroundiphone.jpg")
        break
    case 1: ChangingBackground.image = UIImage(named: "backgroundiphone2.jpg")
        break
    case 2: ChangingBackground.image = UIImage(named: "backgroundiphone3.jpg")
        break
    case 3: ChangingBackground.image = UIImage(named: "backgroundiphone4.jpg")
        break
    case 4: ChangingBackground.image = UIImage(named: "backgroundiphone5.jpg")
        break
    case 5: ChangingBackground.image = UIImage(named: "backgroundiphone6.jpg")
        break
    case 6: ChangingBackground.image = UIImage(named: "backgroundiphone7.jpg")
        break
    case 7: ChangingBackground.image = UIImage(named: "backgroundiphone8.jpg")
        break
    case 8: ChangingBackground.image = UIImage(named: "backgroundiphone9.jpg")
        break
    case 9: ChangingBackground.image = UIImage(named: "backgroundiphone10.jpg")
        break
    case 10: ChangingBackground.image = UIImage(named: "backgroundiphone11.jpg")
        break
    case 11: ChangingBackground.image = UIImage(named: "backgroundiphone12.jpg")
        break
    case 12: ChangingBackground.image = UIImage(named: "backgroundiphone13.jpg")
        break
    case 13: ChangingBackground.image = UIImage(named: "backgroundiphone14.jpg")
        break
    case 14: ChangingBackground.image = UIImage(named: "backgroundiphone15.jpg")
        break
    case 15: ChangingBackground.image = UIImage(named: "backgroundiphone16.jpg")
        break
    case 16: ChangingBackground.image = UIImage(named: "backgroundiphone17.jpg")
        break
    case 17: ChangingBackground.image = UIImage(named: "backgroundiphone18.jpg")
        break
    case 18: ChangingBackground.image = UIImage(named: "backgroundiphone19.jpg")
        break
    case 19: ChangingBackground.image = UIImage(named: "backgroundiphone20.jpg")
        break

    default:
        break;

    }

}


@IBAction func MotivateButtonTapped(sender: UIButton) {

    let quote = randomQuote()
    QuotesLabel.text = quote
    QuotesLabel.textColor = UIColor.whiteColor()
    QuotesLabel.font = UIFont.systemFontOfSize(30)
    QuotesLabel.text = QuotesLabel.text?.uppercaseString
    QuotesLabel.numberOfLines = 0

    // random background image
    var random = arc4random_uniform(20)
    switch(random){

    case 0: ChangingBackground.image = UIImage(named: "backgroundiphone.jpg")
        break
    case 1: ChangingBackground.image = UIImage(named: "backgroundiphone2.jpg")
        break
    case 2: ChangingBackground.image = UIImage(named: "backgroundiphone3.jpg")
        break
    case 3: ChangingBackground.image = UIImage(named: "backgroundiphone4.jpg")
        break
    case 4: ChangingBackground.image = UIImage(named: "backgroundiphone5.jpg")
        break
    case 5: ChangingBackground.image = UIImage(named: "backgroundiphone6.jpg")
        break
    case 6: ChangingBackground.image = UIImage(named: "backgroundiphone7.jpg")
        break
    case 7: ChangingBackground.image = UIImage(named: "backgroundiphone8.jpg")
        break
    case 8: ChangingBackground.image = UIImage(named: "backgroundiphone9.jpg")
        break
    case 9: ChangingBackground.image = UIImage(named: "backgroundiphone10.jpg")
        break
    case 10: ChangingBackground.image = UIImage(named: "backgroundiphone11.jpg")
        break
    case 11: ChangingBackground.image = UIImage(named: "backgroundiphone12.jpg")
        break
    case 12: ChangingBackground.image = UIImage(named: "backgroundiphone13.jpg")
        break
    case 13: ChangingBackground.image = UIImage(named: "backgroundiphone14.jpg")
        break
    case 14: ChangingBackground.image = UIImage(named: "backgroundiphone15.jpg")
        break
    case 15: ChangingBackground.image = UIImage(named: "backgroundiphone16.jpg")
        break
    case 16: ChangingBackground.image = UIImage(named: "backgroundiphone17.jpg")
        break
    case 17: ChangingBackground.image = UIImage(named: "backgroundiphone18.jpg")
        break
    case 18: ChangingBackground.image = UIImage(named: "backgroundiphone19.jpg")
        break
    case 19: ChangingBackground.image = UIImage(named: "backgroundiphone20.jpg")
        break

    default:
        break;

    }




}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


// here is the image

https://drive.google.com/file/d/0B2z_d4wEKPEFMU5lSjBqYTJtX1k/view?usp=sharing

Upvotes: 1

Views: 108

Answers (1)

zaph
zaph

Reputation: 112857

Since the images are 4425 * 2950 and given 4 bytes perplex each one will require 52+ MB when rendered. There are a couple of options:

  1. If the images are part of the app reduce the size prior adding to the app, use any of a number of graphics programs to do this, I use Graphic Converter.

  2. If pre-reduction in size is not possible as soon as loading scale the image, put this code inside an autorelease pool autoreleasepool { scalling code } so the temporary memory will be released as soon as possible.

  3. If you need the images at this size for example panning, and since the size is substantially larger than the display area create tiles prior to adding to the project and use a tiling algorithm, an example of this is Apple Maps.

Upvotes: 1

Related Questions