当我在模拟器上运行我的应用程序时,它运行正常。但是,当我在我的iPhone上运行它时,该应用程序将正常工作,但有时会暂停和崩溃?我该如何解决这个问题?我在哪里可以诊断问题?
我在转换主页的背景中包含了图像。我使用了仪器,在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
}
答案 0 :(得分: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")!]
基本上你是在说,&#34;加载所有这些图像并同时按住它们。&#34;图像巨大,所以你自然会耗尽内存。
制作一个图像名称的数组很好,因为名字只是很小的字符串;那么为什么不这样做呢?然后只在你真正需要它时才加载一个,以便在界面中显示它。