【问题标题】:Xcode 6-Running app on device crashesXcode 6-在设备上运行应用程序崩溃
【发布时间】:2015-01-24 22:42:18
【问题描述】:

当我在模拟器上运行我的应用程序时,它运行良好。但是,当我在我的 iPhone 上运行它时,应用程序可以正常工作,但有时它会暂停并崩溃?我该如何解决这个问题?我可以去哪里诊断问题?

我在过渡的主页的背景中加入了图像。我使用了 Instruments,在 Allocations->Category 下它使用了来自 PNG_Data 的大量内存。我只是在我的 Xcode 项目中包含了大约 12 张 .png 照片。下面是使用图像的代码。

extension Array {
func shuffled() -> [T] {
    var list = self
    for i in 0..<(list.count - 1) {
        let j = Int(arc4random_uniform(UInt32(list.count - i))) + i
        swap(&list[i], &list[j])
    }
    return list
}
}

import Foundation

import UIKit

class ViewController: UIViewController  {

@IBAction func unwindSegue(segue: UIStoryboardSegue) {

}   
@IBOutlet weak var imageView: UIImageView!

// Array of images
let images = [
    UIImage(named: "nature1@2x.png")!,
    UIImage(named: "nature2@2x.png")!,
    UIImage(named: "desk2@2x.png")!,
    UIImage(named: "new_york8@2x.png")!,
    UIImage(named: "new_york9@2x.png")!,
    UIImage(named: "new_york10@2x.png")!,
    UIImage(named: "new_york11@2x.png")!,
    UIImage(named: "new_york14@2x.png")!,
    UIImage(named: "new_york15@2x.png")!,
    UIImage(named: "rainy_window1@2x.png")!,
    UIImage(named: "rainy_window2@2x.png")!,
    UIImage(named: "new_york16@2x.png")!,
    UIImage(named: "new_york17@2x.png")!,
    UIImage(named: "wall_st2@2x.png")!]

// Setting durations and intervals for transitions
var index = 0
let animationDuration: NSTimeInterval = 1.5
let switchingInterval: NSTimeInterval = 4


// VIEW DID LOAD
override func viewDidLoad() {
    super.viewDidLoad()

    imageView.image = images[index++]
    animateImageView()


}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    UIView.animateWithDuration(2.5, animations: {()-> Void in
        self.logo.alpha = 1.0})
}

// Function that animates the background
func animateImageView() {
    CATransaction.begin()

    CATransaction.setAnimationDuration(animationDuration) // passing in animationDuration
    CATransaction.setCompletionBlock {
        let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(self.switchingInterval * NSTimeInterval(NSEC_PER_SEC)))
        dispatch_after(delay, dispatch_get_main_queue()) {
            self.animateImageView()
        }
    }

    let transition = CATransition()
    transition.type = kCATransitionFade // the type of transition

    var new_images = images.shuffled() // SHUFFLING the images array

    imageView.layer.addAnimation(transition, forKey: kCATransition)
    imageView.image = new_images[index]

    CATransaction.commit()

    index = index < images.count - 1 ? index + 1 : 0
}

【问题讨论】:

  • 您必须添加错误以及它出现在代码中的位置。否则我们帮不了你。
  • 在控制台中它只是说“收到内存警告”。
  • 添加了上面的代码。不知道为什么图像使用这么多内存。我应该将图像文件夹放在其他地方吗?我只是将它们放在我的 Xcode 项目中。在使用图像时,我的代码效率不高吗?
  • 你有所有的 i.ages 吗?有时模拟器会创建大缓存并从那里重新加载图像。
  • 图片是否需要在资产目录中,或者它们可以在我的 Xcode 项目的文件夹中?

标签: ios iphone xcode swift crash


【解决方案1】:

这是一件非常愚蠢的事情:

let images = [
    UIImage(named: "nature1@2x.png")!,
    UIImage(named: "nature2@2x.png")!,
    UIImage(named: "desk2@2x.png")!,
    UIImage(named: "new_york8@2x.png")!,
    UIImage(named: "new_york9@2x.png")!,
    UIImage(named: "new_york10@2x.png")!,
    UIImage(named: "new_york11@2x.png")!,
    UIImage(named: "new_york14@2x.png")!,
    UIImage(named: "new_york15@2x.png")!,
    UIImage(named: "rainy_window1@2x.png")!,
    UIImage(named: "rainy_window2@2x.png")!,
    UIImage(named: "new_york16@2x.png")!,
    UIImage(named: "new_york17@2x.png")!,
    UIImage(named: "wall_st2@2x.png")!]

基本上,您是在说,“加载所有这些图像并同时保留它们。”图片是巨大的,所以你的内存自然会用完。

制作一个图像数组names 很好,因为名称只是很小的字符串;那么为什么不这样做呢?然后仅在您真正需要时加载一个,以在界面中显示它。

【讨论】:

  • 我如何通过调用图像名称来做到这一点?在原始帖子的代码中,我有数组并遍历它。图片不是被缓存了吗?我对如何避免使用这么多内存感到困惑。
  • 迭代名称,然后仅加载该图像。不保留任何图像。
  • 如何不保留任何图像?在我的 animateImageView 函数中,我有 imageView.image = new_images[index]。我不知道如何避免在遍历字符串数组时保留图像?
  • 是的,但是在下一次迭代中,您会再说一遍 - 从而释放之前存在的图像!因此,您一次只能保留一张图像。这与一次保留 14 张图像之间存在 巨大 差异。 - 此外,保留它的不是你;它将被保留,因为它实际上正在被使用(它正在显示在界面中)。
  • 还要确保您的图像不超过显示所需的大小。显示大于所需的图像会浪费大量内存。
猜你喜欢
  • 1970-01-01
  • 2023-04-01
  • 2011-12-24
  • 1970-01-01
  • 1970-01-01
  • 2011-04-08
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
相关资源
最近更新 更多