如何阻止我的快速应用程序崩溃 [英] How can I stop my swift app from crashing

查看:374
本文介绍了如何阻止我的快速应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Swift编程的新手,对所有专家寄予厚望。在创建一个简单的应用程序时,我遇到了内存问题,我的应用程序崩溃了。以下是有关我的应用程序的更多详细信息..

I am new to Swift programming and really have high hopes from all the experts out there. While creating a simple app I ran into memory issues and my app crashed. Below are more details about my app..

我的应用程序在工具栏中有一个图像视图,三个图像和三个按钮。我现在所有的应用程序都是按下按钮更改图像。例如,如果按下说按钮1,它将加载图像1,如果按下按钮2则会加载图像2,如果按下按钮3则会加载图像3.

My app has an image view, three images and three buttons in toolbar. All my app does right now is changes image with button press. Example if you press say button1, it will load image1, if you press button2 it would load image 2 and if you press button 3 it would load image 3.

此应用程序在一个周期后崩溃,就像我可以点击所有三个按钮一样,它会起作用,但如果我点击任何按钮之后就会崩溃。

This app crash after 1 cycle, like I can tap all the three buttons and it would work, however if I tap on any button after that it would just crash.

我正在测试这个在iPad mini上。在模拟器上工作得很好。任何帮助将不胜感激。我去某处UIImage(名为:)可能是一个问题,但我不知道如何用Swift中的其他东西替换它。

I am testing this on iPad mini. Works perfectly fine on simulator. Any help would be greatly appreciated. I head somewhere that UIImage (named:) can be an issue, but I don't know how to replace that with anything else in Swift.

以下是代码

import UIKit

class ViewController: UIViewController
{
   @IBOutlet weak var TestImage: UIImageView!

   @IBAction func Button1(sender: AnyObject)
   {
      TestImage.image = UIImage (named: "Image1.jpg")

   }

   @IBAction func Button2(sender: AnyObject)
   {
      TestImage.image = UIImage (named: "Image2.jpg")
   }

   @IBAction func Button3(sender: AnyObject)
   {
      TestImage.image = UIImage (named: "Image1.jpg")
   }

   override func viewDidLoad()
   {
      super.viewDidLoad()
      // Do any additional setup after loading the view, typically from a nib.
   }

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


推荐答案

UIImage(命名:name)的问题在于它是否缓存图像。正如文档说:

The problem with UIImage(named: name) is that it caches the images. As the documentation says:


如果您的图像文件只显示一次,并希望确保它如果没有添加到系统的缓存中,您应该使用 imageWithContentsOfFile:创建图像。这样可以将您的一次性图像保留在系统图像缓存之外,从而可能改善应用程序的内存使用特性。

If you have an image file that will only be displayed once and wish to ensure that it does not get added to the system’s cache, you should instead create your image using imageWithContentsOfFile:. This will keep your single-use image out of the system image cache, potentially improving the memory use characteristics of your app.

因此,如果系统缓存中的图像大小/数量引起内存问题,可以使用 UIImage(contentsOfFile:path)而不是 UIImage( named:name)

Thus, if you have memory problems arising from the size/number of images in the system cache, you can use UIImage(contentsOfFile: path) rather than UIImage(named: name):

if let path = NSBundle.mainBundle().pathForResource("Image1", ofType: "jpg") {
    TestImage.image = UIImage(contentsOfFile:path)
}

这篇关于如何阻止我的快速应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆