由于内存错误导致应用终止

时间:2015-08-01 05:42:19

标签: xcode swift uiimage

一切都很好但是当我将我的应用程序与我的设备连接时。

通过点击按钮看到一些图像后出现此错误。

我正在使用许多图像,因此当人们点击按钮时,它会显示随机的不同图像。

我不知道为什么会出现这个错误,我看了很多类似的问题,但没有找到任何解决方案。

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

1 个答案:

答案 0 :(得分:1)

由于图像是4425 * 2950并且给出4个字节的perplex,每个渲染时将需要52+ MB。有几种选择:

  1. 如果图片是应用程序的一部分,请在添加到应用程序之前缩小尺寸,使用任意数量的图形程序来执行此操作,我使用Graphic Converter

  2. 如果加载缩放图像后无法预先缩小尺寸,请将此代码放入自动释放池autoreleasepool { scalling code }中,以便尽快释放临时内存。

  3. 如果您需要此尺寸的图像进行平移,并且由于尺寸远大于显示区域,因此在添加到项目之前创建切片并使用切片算法,例如Apple Maps